0

I'm fairly new to R. I'm trying to do a sentiment analysis using the bing lexicon. I've tried to find a solution for my problem, but I wasn't able to apply it to my data.

I'm trying to count the positive words in my data, which consists of 401 hotel reviews. Each review has its own row. I want my output to be columns of the review ID, the review itself, and the count of how many positive words are for each ID.

This is my code so far.

pos.wordcount <- function(id,review,count)  
{
  for(i in 1:401)
    reviews.pos <- Corpus(VectorSource(reviews$Reviews[1]))
    id <- c(1:401)
    sentence <- reviews$Reviews[1:401]
    id.reviews <- data.frame(id,sentence)
    pos.wordcount <- str_count(sentence,as.character(bing.pos))
    return(pos.wordcount)
  
}

I haven't been able to go much further because I get this error: Error: no function to return from, jumping to top level

ponyo
  • 1
  • 1
  • The error means that you ran the `return` command outside of a function. Perhaps you ran that line by itself in the console? Or there is additional code which has a syntax error? In addition: running a loop inside a function is not the correct approach as it will return only the last value from the loop. – neilfws Oct 11 '22 at 00:41
  • 1
    Generally, if you set up a loop `for(i in ...)` and then you don't use `i` inside the loop, it's usually a mistake. What do you want to be different at each iteration? And make sure you use `{}` to indicate the code that is part of the loop, otherwise only the next line will be run as part of the loop. – Gregor Thomas Oct 11 '22 at 01:09

0 Answers0