Search code examples
rfor-loopmultiple-columnssplitstrsplit

strsplit into a variable number of columns


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!


Solution

  • 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