[英]Not able to add columns to dataframe R
I want to do keyword matching and then make a data frame before writing it to a CSV file. 我想进行关键字匹配,然后在将数据帧写入CSV文件之前制作一个数据帧。 I declare the data frame as follows -
我声明数据框如下-
outFrame <- data.frame(word1=integer(),
word2=integer(),
word3=integer())
Then I run it over my dictionary - 然后我在字典上运行它-
for (i in 1:NCOL(myKeywords)) {
datadtm <- DocumentTermMatrix(data, control=list(tokenize=BigramTokenizer, wordLengths= c(1,Inf), dictionary = myKeywords[,i]))
datam <- as.matrix(datadtm)
newmat <- rowSums(datam)
outFrame <- cbind2(outFrame, newmat)
}
But I'm getting an error - 但是我遇到一个错误-
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 0, 999
I can see that it's doing the matching correctly but I'm having trouble saving each column to the outFrame
data frame. 我可以看到它正确地进行了匹配,但是我很难将每一列保存到
outFrame
数据帧中。 How to go around this problem, I googled and tried many things, but every time I get the same error. 如何解决这个问题,我用谷歌搜索并尝试了很多方法,但是每次遇到相同的错误时,我都会尝试。
it can be solved by this: 可以这样解决:
outFrame <- data.frame()
for (i in 1:NCOL(myKeywords)) {
datadtm <- DocumentTermMatrix(data, control=list(tokenize=BigramTokenizer, wordLengths= c(1,Inf), dictionary = myKeywords[,i]))
datam <- as.matrix(datadtm)
newmat <- rowSums(datam)
if(nrow(outFrame)==0){
outFrame=data.frame(newmat)
}else{
outFrame <- cbind2(outFrame, newmat)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.