R data.table - categorical values in one column to binary values in multiple columns [duplicate]

久未见 提交于 2020-01-06 16:20:42

问题


I have one data.table with 2 columns ID and X, where X contains categorical values (a, b, c)

ID X
1  a
2  c
3  b
4  c

I would like to transform X into 3 binary columns where the column names are a, b and c

ID a b c
1  1 0 0
2  0 0 1
3  0 1 0
4  0 0 1

What will be a good way to do this? Thank you!


回答1:


Using dcast from data.table,

dcast(dt, ID ~ X, value.var = 'X', fun = length)
#   ID a b c
#1:  1 1 0 0
#2:  2 0 0 1
#3:  3 0 1 0
#4:  4 0 0 1



回答2:


Here is one option from dplyr/tidyr

library(dplyr)
library(tidyr)
df1 %>% 
     mutate(V1 = 1) %>% 
     spread(X, V1, fill= 0)
#  ID a b c
#1  1 1 0 0
#2  2 0 0 1
#3  3 0 1 0
#4  4 0 0 1


来源:https://stackoverflow.com/questions/38453281/r-data-table-categorical-values-in-one-column-to-binary-values-in-multiple-col

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!