简体   繁体   中英

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

我们可以使用dcast

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

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