I am doing a fairly easy job in R
to convert the factor levels into column names.
Let tmp
be a data.frame:
tmp <- data.frame(x=gl(2,3, labels=letters[24:25]),
y=gl(3,1,6, labels=letters[1:3]),
z=c(1,2,3,3,3,2))
> tmp
x y z
1 x a 1
2 x b 2
3 x c 3
4 y a 3
5 y b 3
6 y c 2
My purpose is to make levels in y
column into column names and put 1 in the corresponded column like below:
x y z a b c
x a 1 1 0 0
x b 2 0 1 0
x c 3 0 0 1
y a 3 1 0 0
y b 3 0 1 0
y c 2 0 0 1
Note that what I need is different from dcast
or spread
function in the R package reshape2
and tidyr