简体   繁体   中英

Split dataframe character into columns based on value of character

Is there a way to split data based on value of character in dataframe into multiple columns, so that for example, I start with this data frame

initialData = data.frame(attr = c('a','b','c','d'), type=c('1,2','2','3','2,3'))

And the endData is something like this:

    attr    Conditions  Cond1   Cond2   Cond3
1   a       1,2         TRUE    TRUE    FALSE
2   b       2           FALSE   TRUE    FALSE
3   c       3           FALSE   FALSE   TRUE
4   d       2,3         FALSE   TRUE    TRUE

I've written a function that takes in a character, does a regexp on it to see if the condition is met and then returns true or false, but I'm not sure how to go through each line in the data frame and add to the correct column

We can use mtabulate from qdapTools after splitting the 'type' column with strsplit and cbind with the original dataset

library(qdapTools)
out <- cbind(initialData, 
     mtabulate(strsplit(as.character(initialData$type), ",")) > 0)
names(out)[3:5] <- paste0("Cond", names(out)[3:5])
out
#   attr type Cond1 Cond2 Cond3
#1    a  1,2  TRUE  TRUE FALSE
#2    b    2 FALSE  TRUE FALSE
#3    c    3 FALSE FALSE  TRUE
#4    d  2,3 FALSE  TRUE  TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM