Problem when combining group_by and mutate

Hey! I am trying to combine the functions group_by with mutate but after the mutation, the result turns out not to be grouped.
These are the commands that I am using:
oecd_relative <- oecd_joined %>%
group_by(Year,country_code)%>%
mutate(PW_REAL_PPP = PW_REAL_PPP-mean(PW_REAL_PPP),PC_REAL_PPP = PC_REAL_PPP-mean(PC_REAL_PPP),REAL_PPP = REAL_PPP-mean(REAL_PPP))

Does anyone know what can be wrong? I want to group my mutation by year and country.

I think its a confusion of your involving grouped by and non grouped by elements in the same calculation.
Why not summarised oecd_joined by group_by and find the relevant means put them in means_ variables.
Then join the result to oecd_joined and substract each value from the mean

Thank you for your answer! Indeed that is a good thought. The reason why I focused on these two commands only is that my task allows me to only use these two commands so I am trying to see how to use them both in a correct way in order to get the result.

Hi!

The problem in your specific example is that you are using same variable in mutate: x = x-mean(x).

Good way to deal with this specific task is to use mutate_at. Example:

iris %>% 
  group_by(Species) %>% 
  mutate_at(c("Sepal.Length", "Sepal.Width"), (function(x) x - mean(x)))

This code will take 2 indicated columns and replace their values with deviations from the mean per group.

1 Like

on second thoughts, there wouldnt seem to be anything especially wrong with the original approach.
I'm thinking that Ioanna might have suspected it wasnt working as it should , even though it was working fine.

(iris %>% 
    group_by(Species) %>% mutate(
      Sepal.Length = Sepal.Length-mean(Sepal.Length))-> idf2)

(iris %>% 
  group_by(Species) %>% 
  mutate_at(c("Sepal.Length", "Sepal.Width"), (function(x) x - mean(x))) ->idf1)

identical(idf1$Sepal.Length,idf2$Sepal.Length)

Thank you! Indeed after all it was working fine I just had the thought that it was not.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.