Search code examples
rdummy-data

Creating Dummy Variables from List


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?


Solution

  • 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