从列表创建虚拟变量

Creating Dummy Variables from List

所以我正在尝试创建虚拟变量以根据框架的特定列是否包含特定单词来附加到数据框。该列看起来像这样:

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

我会根据每行中包含的单词创建虚拟变量,例如对于第一个,它包含 "good", "night","moon",但不包含 "room", "morning""hello"

到目前为止,我一直采用的方法是创建一个大小合适的 0 值矩阵,然后像这样使用 for 循环:

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

或类似的东西。我猜有一种 faster/more 资源高效的方法可以做到这一点。有什么建议吗?

我愿意

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

对于列中的虚拟变量(R 约定),结果可以用 t(res) 转置。

你可以试试:

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

给出:

#         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

如果你想要列中的虚拟变量,你可以使用 DocumentTermMatrix 代替:

#    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

尝试

 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

或者

 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