简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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