1

I have this data frame

 d              f         
 "first tweet"  A
 "second tweet" B
 "thrid tweet"  C 

And I would like to get this

 d              A      B       C         
 "first tweet"  1      0       0
 "second tweet" 0      1       0
 "thrid tweet"  0      0       1

Thanks!

JohnCoene
  • 2,107
  • 1
  • 14
  • 31
  • 1
    Is there any real reason you need this? R tends to prefer factors to dummy-variable style coding. – MrFlick Jul 07 '14 at 04:03

3 Answers3

5

Here are a few options to consider:

  1. model.matrix

    cbind(mydf, model.matrix(~ 0 + f, data = mydf))
    #              d f fA fB fC
    # 1  first tweet A  1  0  0
    # 2 second tweet B  0  1  0
    # 3  thrid tweet C  0  0  1
    
  2. table

    cbind(mydf, as.data.frame.matrix(table(sequence(nrow(mydf)), mydf$f)))
    #              d f A B C
    # 1  first tweet A 1 0 0
    # 2 second tweet B 0 1 0
    # 3  thrid tweet C 0 0 1
    
  3. dcast from "reshape2"

    library(reshape2)
    dcast(mydf, d ~ f, value.var="f", fun.aggregate=length)
    #              d A B C
    # 1  first tweet 1 0 0
    # 2 second tweet 0 1 0
    # 3  thrid tweet 0 0 1
    

Note that there is a difference between the first two options and the third one. The third option will collapse (and tabulate) values if there happen to be repeated values for column "d", while the first two split the values out on a row-by-row basis.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
1

Another possibility:

library(qdap)
mtabulate(split(dat[[2]], dat[[1]]))

##              A B C
## first tweet  1 0 0
## second tweet 0 1 0
## thrid tweet  0 0 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
1

A very simple table seems like it might do the trick.

> d <- data.frame(d = c("first tweet", "second tweet", "third tweet"),
                  f = c("A", "B", "C"))
> tab <- table(d)
> data.frame(d = rownames(tab), tab[,1:3], row.names = NULL)
#              d A B C
# 1  first tweet 1 0 0
# 2 second tweet 0 1 0
# 3  third tweet 0 0 1
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245