0

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!

qwertyl
  • 87
  • 2
  • 10
  • 2
    See `?table` (`table(data)`) and, probably, [this](http://stackoverflow.com/questions/7442207/contingency-table-with-r) might help – alexis_laz Jul 19 '16 at 08:43

2 Answers2

2

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
Sotos
  • 51,121
  • 6
  • 32
  • 66
0

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
akrun
  • 874,273
  • 37
  • 540
  • 662