Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following function defined in R:

CreateTestFromRaw <- function(text, dict) {
  _corpus <- Corpus(VectorSource(text))
  corpus_clean <- tm_map(_corpus, removeNumbers)
  corpus_clean <- tm_map(corpus_clean, removeWords, stopwords('english'))
  corpus_clean <- tm_map(corpus_clean, removePunctuation)
  corpus_clean <- tm_map(corpus_clean, stripWhitespace)
  #dtm <- DocumentTermMatrix(corpus_clean)
  test <- DocumentTermMatrix(corpus_clean, control = list(dictionary = dict))
  test <- apply(test, MARGIN = 2, convert_counts)
  return test
}

I am using the text mining package:

install.packages("tm")
library(tm)

When I try to run the code above to create the function, it gives me these errors:

> CreateTestFromRaw <- function(text, dict) {
+   _corpus <- Corpus(VectorSource(text))
Error: unexpected input in:
"CreateTestFromRaw <- function(text, dict) {
  _"
>   corpus_clean <- tm_map(_corpus, removeNumbers)
Error: unexpected input in "  corpus_clean <- tm_map(_"
>   corpus_clean <- tm_map(corpus_clean, removeWords, stopwords('english'))
>   corpus_clean <- tm_map(corpus_clean, removePunctuation)
>   corpus_clean <- tm_map(corpus_clean, stripWhitespace)
>   #dtm <- DocumentTermMatrix(corpus_clean)
>   test <- DocumentTermMatrix(corpus_clean, control = list(dictionary = dict))
Error in stopifnot(is.list(control)) : object 'dict' not found
>   test <- apply(test, MARGIN = 2, convert_counts)
Error in apply(test, MARGIN = 2, convert_counts) : 
  object 'test' not found
>   return test
Error: unexpected symbol in "  return test"
> }
Error: unexpected '}' in "}"

Why do I get these errors when trying to create this function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
272 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can't start a variable name with an underscore in R. Also, return is a function in R, so you must write it as return(test).

R is very different from most programming languages, so I wouldn't recommend jumping right into it without learning the main differences first.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...