0

Based on the code and data (obtained from rayshader tutorial website) provided in the post, I am getting the error shown below, even though the object Year does exist as a column in the dataframe, melt_measles.

Why is it so and how can it be fixed?

Error:

Error in ifelse(Year == 2019 & !(Month %in% c("January", "February", "March")),  : 
  object 'Year' not found

Code:

library(tidyverse)
library(reshape2)

measles = read_csv("https://tylermw.com/data/measles_country_2011_2019.csv")
melt_measles = melt(measles, id.vars = c("Year", "Country", "Region", "ISO3"))
melt_measles$Month = melt_measles$variable
melt_measles$cases = melt_measles$value
melt_measles %>% 
  group_by(Year, Month) %>%
  summarize(totalcases = sum(cases,na.rm = TRUE)) %>% 
  mutate(totalcases = ifelse(Year == 2019 & !(Month %in% c("January","February","March")), NA, totalcases))
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
  • 4
    I do not get this error. I have two ideas: 1. After `summarize(totalcases = sum(cases,na.rm = TRUE)) %>%` this line add a line with `ungroup()`. 2. Change `ifelse` by `if_else`. A 3. idea clean workspace and restart R. – TarJae Nov 21 '22 at 19:09
  • 2
    I edited to trim down the code to the relevant part, and declare needed packages, I can run this code. You might take a look at https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example to help us help you better, and especially the concept of minimal reproducible example. – moodymudskipper Nov 21 '22 at 19:12
  • 2
    @TarJae, step 3 (did not apply the first 2 steps) fixed sort of fixed it as I am not getting this error now, so thank you for that. But now I am getting an error related to `rayshader` onto the next post I guess. – Ed_Gravy Nov 21 '22 at 19:19

0 Answers0