Home > Software engineering >  Trying to sum a num column, and getting "only defined on a data frame with all numeric-alike va
Trying to sum a num column, and getting "only defined on a data frame with all numeric-alike va

Time:08-17

This Project researches Google trends in gaming. To be more specific, I will be looking into five games that are trending according to Twitch, a famous streaming platform. The five game I've chosen are Cult of the Lamb, Elden Ring, Divinity Original Sin 2, Minecraft, and League of Legends.The goal is to find what exactly is making them such a searched topic on Google, and if the users are combining the game titles with certain terms.

One of the terms is "gameplay". I am using package gtrendsR to collect the number of searches. I use the following code:

library(gtrendsR)
trends_gameplay <- gtrends(keyword=c("Cult of the Lamb gameplay", "Elden Ring gameplay", "Divinity Original Sin 2 gameplay", " Minecraft gameplay", "League of Legends gameplay"))

trends_gameplay_over_time <- trends_gameplay$interest_over_time
trends_gameplay_over_time <- trends_gameplay_over_time %>% mutate(hits = as.numeric(hits))
trends_gameplay_over_time <- trends_gameplay_over_time %>% replace_na(list(hits = 0))

This is the same block of code that I use for other terms. After that, I want to gather the total number of searches, and compare them in a bar graph.

And this is where I'm having a trouble. I was thinking about using the following code block to sum the "hits" in the dataframe:

total_trends_gameplay_over_time <- trends_gameplay_over_time %>%
  sum(trends_gameplay_over_time$hits)

which is giving me the error. I don't understand why, if "hits" column is already in a numeric format

CodePudding user response:

This is happening because your hits column has values such as "<1" in it, which should be replaced by numbers. Also, to sum you just need the second line of your last code chunk:

library(gtrendsR)
library(tidyverse)

trends_gameplay <- gtrends(keyword=c("Cult of the Lamb gameplay", "Elden Ring gameplay", "Divinity Original Sin 2 gameplay", " Minecraft gameplay", "League of Legends gameplay"))

trends_gameplay_over_time <- trends_gameplay$interest_over_time %>%
  as_tibble() %>%
  mutate(hits = stringr::str_replace(hits, "<1", "0"),
         hits = readr::parse_number(hits))

trends_gameplay_over_time %>%
  group_by(keyword) %>%
  summarise(total_hits = sum(hits))

# # A tibble: 5 × 2
#   keyword                            total_hits
#   <chr>                                   <dbl>
# 1 " Minecraft gameplay"                    1957
# 2 "Cult of the Lamb gameplay"                44
# 3 "Divinity Original Sin 2 gameplay"        253
# 4 "Elden Ring gameplay"                     849
# 5 "League of Legends gameplay"              478

sum(trends_gameplay_over_time$hits)

# [1] 3581
  • Related