Search code examples
rvariablesnew-operator

Trouble with creating new variable based on mean in R


enter image description here

I have the following data and I am trying to get the mean duration (dur)

atusJoin3 %>%
    group_by(tuyear, agegroup) %>% 
    summarise(mean_dur = mean(dur, na.rm = T))

enter image description here

However, when I try to use the mean_dur in ggplot, it says that mean_dur is not found. Any idea, why mean_dur cannot be found?


Solution

  • It could be because the object is not updated by assigning (<-) to the same object (but that will overwrite the original object with summarised one, instead it is better to have a new object)

    atusJoin_summary <-  atusJoin3 %>%
             group_by(tuyear, agegroup) %>% 
             summarise(mean_dur = mean(dur, na.rm = TRUE))
    

    Now, we use atusJoin_summary as input data to ggplot


    NOTE: If the intention is to create a new column in the data, use mutate inplace of summarise