0

So as a beginner, I'm trying to do simple text-mining (NLP) using R language.

I preprocessed my data using tm_map function and inspected it and all the punctuations, numbers were removed. I also converted the text document in lower case using tolower() function. It worked great.

But while creating a document matrix, I'm encountering an issue where the error is:

error in tolower(txt): non character argument

What is this error about and how to go ahead with this? Is this something related to UTF8? Any leads would be appreciated.

docs <- tm_map(docs, removePunctuation) 
inspect(docs[1]) 

for(j in seq(docs)) { 
  docs[[j]] <- gsub("\n", " ", docs[[j]]) 
} 

docs <- tm_map(docs, removeNumbers) 
docs <- tm_map(docs, content_transformer(tolower)) 
docs <- tm_map(docs, removeWords, stopwords("english")) 
docs <- tm_map(docs, stripWhitespace) 

This all worked just fine and my text document (which is simply an ebook) got converted into lower case, with no white spaces, numbers, etc. just fine and the next step returns the error.

# returns the above error. 
dtm <- DocumentTermMatrix(docs)
phiver
  • 23,048
  • 14
  • 44
  • 56
  • Hi Rachit, welcome to StackOverflow! It'll help us debug your problem if you can provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It seems likely that one of the entries in the `txt` object isn't a character, but we can't determine which one or why not without more information. – Dubukay Jan 26 '21 at 02:52
  • docs <- tm_map(docs, removePunctuation) inspect(docs[1]) for(j in seq(docs)) { docs[[j]] <- gsub("\n", " ", docs[[j]]) } docs <- tm_map(docs, removeNumbers) docs <- tm_map(docs, content_transformer(tolower)) docs <- tm_map(docs, removeWords, stopwords("english")) docs <- tm_map(docs, stripWhitespace) This all worked just fine and my text document (which is simply an ebook) got converted into lower case, with no white spaces, numbers, etc. just fine and the next step: dtm <- DocumentTermMatrix(docs) returns the above error. – Rachit Singh Jan 26 '21 at 03:32

1 Answers1

0

The problem is not turning your corpus into a DocumentTermMatrix. The problem lies in your for loop. It turns your corpus into a list of characters.

If you want to use gsub like this, you need to use the content_transformer function.

# removes the need of the for loop and keeps everything in a corpus.
docs <- tm_map(docs, content_transformer(function(x) gsub("\n", " ", x)))

This removes the need of the loop and keeps everything as it should be. After this line you can run the rest of your code without any issue.

phiver
  • 23,048
  • 14
  • 44
  • 56
  • Hey, thankyou so much. I'll just run the code again and then let you know about the result. Thankyou :) – Rachit Singh Jan 28 '21 at 00:08
  • Hello phiver, thanks for the suggestion but upon running the changes, it again is showing the same error i.e. the one mentioned in the post. error in tolower(txt): non character argument – Rachit Singh Jan 28 '21 at 20:06
  • @RachitSingh, In that case, can you link to or add an example where the code fails? My tests don't return an error. Without an example it is just guessing what goes wrong. – phiver Jan 29 '21 at 08:44