6

I'm using the tm package to apply stemming, and I need to convert the resulting data into a data frame. A solution for this can be found here R tm package vcorpus: Error in converting corpus to data frame, but in my case I have the content of the corpus as:

[[2195]]
i was very impress

instead of

[[2195]]
"i was very impress"

and because of this, if I apply

data.frame(text=unlist(sapply(mycorpus, `[`, "content")), stringsAsFactors=FALSE)

the result will be

<NA>.

Any help is much appreciated!

Code below as an example:

sentence <- c("a small thread was loose on the sandals, otherwise it looked good")
mycorpus <- Corpus(VectorSource(sentence))
mycorpus <- tm_map(mycorpus, stemDocument, language = "english")

inspect(mycorpus)

[[1]]
a small thread was loo on the sandals, otherwi it look good

data.frame(text=unlist(sapply(mycorpus, `[`, "content")), stringsAsFactors=FALSE)

 text
1 <NA>
Community
  • 1
  • 1

2 Answers2

2

By applying

gsub("http\\w+", "", mycorpus)

the output has class = character, so it works in my case.

  • 1
    I would be helpful if you would write the complete code for the conversion. Otherwise readers are left to the hit and trial method. – Simone May 06 '16 at 19:55
1

I'm unable to reproduce the problem using tm_0.6 in R 3.1.0 on a Mac:

> data.frame(text=unlist(sapply(mycorpus, `[`, "content")), stringsAsFactors=FALSE)
                                                                 text
content a small thread was loos on the sandals, otherwis it look good

If I had gotten those undesired results I would have immediately tried:

 data.frame(text=unlist(sapply(mycorpus, `[[`, "content")), stringsAsFactors=FALSE)

... reasoning that since 'constent' is a list-element name that [['content']] should have been able to so the serial extraction. It also looked to me that the unlist might not be needed with that approach:

> data.frame(text=sapply(mycorpus, `[[`, "content"), stringsAsFactors=FALSE)
                                                           text
1 a small thread was loos on the sandals, otherwis it look good
IRTFM
  • 258,963
  • 21
  • 364
  • 487