3

So I'm trying to create dummy variables to attach to a data frame based on whether or not a specific column of the frame has specific words in it. The column would look something like this:

 dumcol = c("good night moon", "good night room", "good morning room", "hello moon")

and I'd be creating dummy variables based on which words are contained in each row, e.g. for the first one, it contains "good", "night", and "moon", but not "room", "morning" or "hello".

The way I've been going about it so far, in a hugely primitive way, is creating a 0-valued matrix of appropriate size, and then using a for loop like so:

result=matrix(ncol=6,nrow=4)
wordlist=unique(unlist(strsplit(dumcal, " ")))
for (i in 1:6)
{ result[grep(wordlist[i], dumcol),i] = 1 }

or something similar. I'm guessing there's a faster/more resource efficient way to do it. Any advice?

riders994
  • 1,246
  • 5
  • 13
  • 33

3 Answers3

3

You could try:

library(tm)
myCorpus <- Corpus(VectorSource(dumcol))
myTDM <- TermDocumentMatrix(myCorpus, control = list(minWordLength = 1))
as.matrix(myTDM)

Which gives:

#         Docs
#Terms     1 2 3 4
#  good    1 1 1 0
#  hello   0 0 0 1
#  moon    1 0 0 1
#  morning 0 0 1 0
#  night   1 1 0 0
#  room    0 1 1 0

If you want the dummy variables in columns, you could use DocumentTermMatrix instead:

#    Terms
#Docs good hello moon morning night room
#   1    1     0    1       0     1    0
#   2    1     0    0       0     1    1
#   3    1     0    0       1     0    1
#   4    0     1    1       0     0    0
Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
  • 2
    Never heard of that package, but I bet it has a lot of tools for the OP's other tasks. – Frank May 28 '15 at 17:23
  • 3
    Yeah, I've used tm before, it's what I use for word-associations as well. I'm pretty new to it, so learning about its different functions is really helpful. @Steven, does tm treat the DTM as an object that I can attach to a dataframe using cbind? I've had problems with how functions from tm output the data. – riders994 May 28 '15 at 17:28
3

Try

 library(qdapTools)
 mtabulate(strsplit(dumcol, ' '))
 #    good hello moon morning night room
 #1    1     0    1       0     1    0
 #2    1     0    0       0     1    1
 #3    1     0    0       1     0    1
 #4    0     1    1       0     0    0

Or

 library(splitstackshape)
 cSplit_e(as.data.frame(dumcol), 'dumcol', sep=' ', 
                      type='character', fill=0, drop=TRUE)
 #  dumcol_good dumcol_hello dumcol_moon dumcol_morning dumcol_night dumcol_room
 #1           1            0           1              0            1           0
 #2           1            0           0              0            1           1
 #3           1            0           0              1            0           1
 #4           0            1           1              0            0           0
akrun
  • 874,273
  • 37
  • 540
  • 662
2

I would do

sdum <- strsplit(dumcol," ")
us   <- unique(unlist(sdum))
res  <- sapply(sdum,function(x)table(factor(x,levels=us)))
#         [,1] [,2] [,3] [,4]
# good       1    1    1    0
# night      1    1    0    0
# moon       1    0    0    1
# room       0    1    1    0
# morning    0    0    1    0
# hello      0    0    0    1

The result can be transposed with t(res) for the dummy variables in columns (the R convention).

Frank
  • 66,179
  • 8
  • 96
  • 180
  • 1
    Thanks! This will definitely work, but I might try one of the other solutions because I can use the results for other things as well. – riders994 May 28 '15 at 17:36