Sorrentum Sorrentum - 9 days ago 4x
R Question

Create dummy variables for non excluding categories in R

I want to create a binary representation of categories. I know how to do it for excluding categories (using caret package) but not an straighforward way to do it for non-excluding categories. For instance

movies <- data.table(movie=c( "batman", "bighero6"), type=list("action",c("action","animation")))
movie type
1: batman action
2: bighero6 action,animation

I would like to obtain something like

action animation
batman 1 0
bighero6 1 1


We can use dcast

 dcast(movies[,.(type=unlist(type)) ,movie], movie~type, length)