prog
prog

Reputation: 1073

Converting corpus to dataframe returns NA's

I am trying to convert my corpus back to a dataframe but it returns only NA's. Please Help

Code:

library(wordcloud)
df <- data.frame(Description = c("I like this service very much"," this is worth it so much"))

corpus <- Corpus(VectorSource(df$Description))

corpus <- tm_map(corpus, tolower)

corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeWords, c(stopwords('english')))
corpus <- tm_map(corpus, stripWhitespace)
corpus <- tm_map(corpus, stemDocument)

#a <- as.data.frame(corpus)

dataframe<-data.frame(text=unlist(sapply(corpus, `[`, "content")), 
                      stringsAsFactors=F)

Expected Output:

dataframe

'like servic much'
'worth much'

Upvotes: 1

Views: 151

Answers (2)

Seema
Seema

Reputation: 7

The below code worked for me too.

dataframe <- data.frame(text = sapply(corpus, as.character), stringsAsFactors = FALSE)

Input:

df <- data.frame(Description = c("I like to travel. It expands my outlook. I have visited 12 National Parks."))

Output:

like travel expand outlook visit 12 nation park

Upvotes: 0

prog
prog

Reputation: 1073

This below code worked for me:

dataframe <- data.frame(text = sapply(corpus, as.character), stringsAsFactors = FALSE)

Output:

              text
1 like servic much
2       worth much

Upvotes: 0

Related Questions