簡體   English   中英

無法將列添加到數據框R

[英]Not able to add columns to dataframe R

我想進行關鍵字匹配,然后在將數據幀寫入CSV文件之前制作一個數據幀。 我聲明數據框如下-

outFrame <- data.frame(word1=integer(),
                       word2=integer(),
                       word3=integer())

然后我在字典上運行它-

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)
}

但是我遇到一個錯誤-

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 0, 999

我可以看到它正確地進行了匹配,但是我很難將每一列保存到outFrame數據幀中。 如何解決這個問題,我用谷歌搜索並嘗試了很多方法,但是每次遇到相同的錯誤時,我都會嘗試。

可以這樣解決:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM