问题
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