简体   繁体   English

从列表创建虚拟变量

[英]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, eg for the first one, it contains "good", "night", and "moon" , but not "room", "morning" or "hello" . 我将根据每行中包含的单词创建虚拟变量,例如,对于第一行,它包含"good", "night","moon" ,但不包含"room", "morning""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: 到目前为止,我以一种非常原始的方式进行处理的方法是创建一个适当大小的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 }

or something similar. 或类似的东西。 I'm guessing there's a faster/more resource efficient way to do it. 我猜这是一种更快/更资源有效的方法。 Any advice? 有什么建议?

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: 如果您想在列中使用虚拟变量,则可以使用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

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

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). 结果可以用t(res)为列中的虚拟变量(R约定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM