0

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

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
MyQ
  • 459
  • 3
  • 13

1 Answers1

2

We can use mtabulate from qdapTools

cbind(tmp, qdapTools::mtabulate(tmp$y))
#  x y z a b c
#1 x a 1 1 0 0
#2 x b 2 0 1 0
#3 x c 3 0 0 1
#4 y a 3 1 0 0
#5 y b 3 0 1 0
#6 y c 2 0 0 1

Or cSplit_e from splitstackshape

splitstackshape::cSplit_e(tmp, "y", type = "character", fill = 0)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213