我有一个小型语料库,例如
myvec <- c("n417", "disturbance", "grand theft auto", "assault", "burglary",
"vandalism", "atmt to locate", "drug arrest", "traffic stop",
"larceny", "graffiti complaint / reporting")
corpus <- VCorpus(VectorSource(myvec))
如果我想要将语料库大10倍,我该如何做到这一点,以便生成的变量是VCorpus而不是列表?
尝试:
corpus <- replicate(10, corpus) # returns a list
corpus <- VCorpus(replicate(10, corpus)) # Error: inherits(x, "Source") is not TRUE
corpus <- c(corpus, corpus, corpus, corpus, corpus, corpus, corpus) # works, returns a corpus 7 times bigger but involves lots of typing)
如果我有一个小型语料库,并且我想为了示例目的而使其大十倍,我该怎么办呢?
答案 0 :(得分:2)
我们可以在复制后使用do.call
c
library(tm)
do.call(c, rep(list(corpus), 7))
# <<VCorpus>>
#Metadata: corpus specific: 0, document level (indexed): 0
#Content: documents: 77
同样适用于replicate
do.call(c, replicate(7, corpus, simplify = FALSE))
#<<VCorpus>>
#Metadata: corpus specific: 0, document level (indexed): 0
#Content: documents: 77
此处simplify = FALSE
replicate
do.call(c, replicate(7, corpus))
#<<VCorpus>>
#Metadata: corpus specific: 0, document level (indexed): 0
#Content: documents: 77