如何将列名添加到基于矩阵的数据框的第一列?

时间:2016-04-18 14:28:35

标签: r matrix dataframe

如何将列名添加到基于矩阵的数据框的第一列?

 web_page <- readLines("http://apiolaza.net/babel/")
   library(tm)
   SampCrps<- Corpus(VectorSource(web_page))
   corp <- tm_map(SampCrps, PlainTextDocument)
   dtm <-DocumentTermMatrix(oz)
   findFreqTerms(dtm,2) # words that apear at least 2 times
   dtmMatrix <- as.matrix(dtm) 
   wordsFreq <- colSums(dtmMatrix)
   wordsFreq <- sort(wordsFreq, decreasing=TRUE)
   head(wordsFreq,10)
library     one     the    book   books   whose another     can letters   first 
     23      19      16      13      13       9       8       8       8       7 

    wordsFreq<-as.data.frame(wordsFreq)
 wordsFreq<-as.data.frame(wordsFreq)
 head(wordsFreq)
        wordsFreq
library        23
one            19
the            16
book           13
books          13
whose           9

然而,当我试图命名第一列时,我收到了这个错误:

 colnames(wordsFreq)<-c("word", "count")
Error in `colnames<-`(`*tmp*`, value = c("word", "count")) : 
  'names' attribute [2] must be the same length as the vector [1]

如何解决此问题,并使用我想要的名称命名第一列

1 个答案:

答案 0 :(得分:1)

而不是wordsFreq<-as.data.frame(wordsFreq)

使用

wordsFreq <-  as.data.frame(wordsFreq)
wordsFreq <- data.frame(word = rownames(wordsFreq), count = wordsFreq, row.names = NULL)