Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

R function unexpected input errors

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...