read.table(text="chicken
chicken,goat
cow,chicken,goat
cow", fill=TRUE, sep=",")
# Trivial to change the names of dataframe columns
V1 V2 V3
1 chicken
2 chicken goat
3 cow chicken goat
4 cow
strsplit into a variable number of columns
-
29-06-2022 - |
Pergunta
This is a follow-on to this question I asked earlier: R for loop: create a new column with the count of a sub str from a different column
I have a large table (100+ colums, 50k+rows). One of the columns contains data in the following format:
col
chicken
chicken,goat
cow,chicken,goat
cow
I want to get to:
col col2 col3
chicken
chicken goat
cow chicken goat
cow
There are many more than 3 columns that need to be filled, I just stripped this down to be an example. My script creates the appropriate number of columns to be filled, I just need the code, I assume it's a for loop, to split the string in 'col' on ',' and then place the split strings into the subsequent columns.
Thanks for any help!
Solução
Outras dicas
You may try this:
library(splitstackshape)
concat.split(data = df, split.col = 1, sep = ",", drop = TRUE)
# col_1 col_2 col_3
# 1 chicken
# 2 chicken goat
# 3 cow chicken goat
# 4 cow
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow