Use strsplit
to split choice
creating s
and give it DF$id
as names. From s
extract a vector of all the levels, all_lev
. Then sapply
a function over s
which creates a factor from each component of s
and runs table
on it. Finally transpose that.
s <- setNames( strsplit(DF$choice, ","), DF$id )
all_lev <- sort(unique(unlist(s)))
m <- t(sapply(s, function(x) table(factor(x, lev = all_lev))))
This gives the following matrix where the row names are the id's:
> m
a b c
1 1 1 1
2 0 0 1
3 1 0 1
4 0 1 1
If you prefer a data frame then using m
above:
data.frame(id = rownames(m), m)
Note 1: If we knew that the levels were always "a"
, "b"
and "c"
then we could hard code all_lev
shortening it to:
s <- setNames( strsplit(DF$choice, ","), DF$id )
m <- t(sapply(s, function(x) table(factor(x, lev = c("a", "b", "c")))))
Note 2: We assumed that DF
was this:
Lines <- 'id choice
----------
1 "a,b,c"
2 "c"
3 "a,c"
4 "b,c"'
DF <- read.table(text = Lines, header = TRUE, comment = "-", as.is = TRUE)
Update Shortened answer.