0

I'm working on some twitter sentiment analysis web app for a project and I found out that when I call the function mutate in order to create a new column for a dataframe i'm working with, it returns "object must be size 1".

The thing is that when I use the function with specific values it works completely fine, but if I use the values for a general case since I want the app to be able to analyze every user you input.

Here's the code for the specific value:

#a) Pivot
   tweets_spread <- tweets_tidy %>% group_by(autor, token) %>% count(token) %>% spread(key=autor, value = n, fill = 0, drop = TRUE)
   tweets_unpivot <- tweets_spread %>% gather(key = "autor", value = "n", -token)
  

  # b) Selection of authors (JoeBiden and BernieSanders)
  tweets_unpivot <- tweets_unpivot %>% 
    filter(autor %in% c(user1clean, user2clean))
  
  # c) Total words per author
  tweets_unpivot <- tweets_unpivot %>% left_join(tweets_tidy %>% group_by(autor) 
                                                 %>%summarise(N = n()), by = "autor")
  tweets_unpivot
  # d) Cálculo de odds y log of odds de cada palabra
  tweets_logOdds <- tweets_unpivot %>% mutate(odds = (n + 1) / (N + 1))
  
  tweets_logOdds
  
  tweets_logOdds <- tweets_logOdds %>% select(autor, token, odds) %>% spread(key = autor, value = odds)
  tweets_logOdds

#HERE'S THE ISSUE, WHEN I INPUT THE SPECIFIC CASE FOR THE AUTHORS, IT WORKS FINE
tweets_logOdds <- tweets_logOdds %>%
    mutate(log_odds = log(JoeBiden/BernieSanders), abs_log_odds = abs(log_odds))
  
  tweets_logOdds

BernieSanders and JoeBiden are the names of the columns in tweets_logOdds, so I figured a way of how to make it for a general case:

 # 
  coluser1 <- tweets_logOdds[user1clean] 
  coluser1
  # 
  coluser2 <- tweets_logOdds[user2clean]
  coluser2
  
  head(log(coluser1/coluser2))
  
  tweets_logOdds <- tweets_logOdds %>%
    mutate(log_odds = log(coluser1/coluser2), abs_log_odds = abs(log_odds))

But this returns the following error:

>   tweets_logOdds <- tweets_logOdds %>%
+     mutate(log_odds = log(coluser1/coluser2), abs_log_odds = abs(log_odds))
Error: Problem with `mutate()` column `log_odds`.
i `log_odds = log(coluser1/coluser2)`.
i `log_odds` must be size 1, not 2975.
i The error occurred in group 1: token = "<U+FE0F>in".

If someone could please help me to solve this, I only have until friday to finish it.

Here's the repository in case you want to check out the whole code: https://github.com/davidrovj/SentimentAnalysisShinyR/blob/master/prueba.R

mm ar
  • 1
  • 1
  • Does this answer your question? [Error: Problem with \`mutate()\` column (...) must be size 15 or 1, not 17192](https://stackoverflow.com/questions/70185381/error-problem-with-mutate-column-must-be-size-15-or-1-not-17192) – Taren Sanders Dec 16 '21 at 07:47
  • 5
    I think this is a good example of a problem that would be solved just by writing a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). That would make it easier to understand the general problem, and then either 1) fix the issue, or 2) create a better question for SO. – Taren Sanders Dec 16 '21 at 07:48

0 Answers0