I have a dataframe like this:
df <- data.frame(V1=c("a,b,c,d,e,f","a,b,c","e,f","b,d","a,e"))
I want to generate all possible dummies according to categories in var V1
, something like this:
df$a <- c(1,1,0,0,1)
df$b <- c(1,1,0,1,0)
df$c <- c(1,1,0,0,0)
df$d <- c(1,0,0,1,0)
df$e <- c(1,0,1,0,1)
df$f <- c(1,0,1,0,0)
> df
V1 a b c d e f
1 a,b,c,d,e,f 1 1 1 1 1 1
2 a,b,c 1 1 1 0 0 0
3 e,f 0 0 0 0 1 1
4 b,d 0 1 0 1 0 0
5 a,e 1 0 0 0 1 0
How can I do this efficiently? I have a big dataframe and V1
has a lot of categories.