Sorrentum Sorrentum - 1 year ago 69
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

Answer Source

We can use dcast

 dcast(movies[,.(type=unlist(type)) ,movie], movie~type, length)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download