group_by subtraction

Dear R users,

I have one data, in which I have to subtract (group wise) some constant numbers from each season:
I mean I have 4 constant values for each season says: DJF=10, MAM=20, JJAS=30,ON=40 ( these constant values are same for each year)

Now for one season I can do via:

> d %>% filter(region=="KARA", season=="DJF") %>% mutate(ano=meanSA-10)

But for group wise, I am not getting how to do that. My data is given as:

   decade region season meanSA stdSA decade1
   <chr>  <chr>  <chr>   <dbl> <dbl>   <dbl>
 1 2020   KARA   DJF     180.  12.4     2020
 2 2020   KARA   JJAS     97.4 11.2     2020
 3 2020   KARA   MAM     298.  15.4     2020
 4 2020   KARA   ON       80.7  7.16    2020
 5 2030   KARA   DJF     172.  19.4     2030
 6 2030   KARA   JJAS     81.5 12.9     2030
 7 2030   KARA   MAM     278.  29.2     2030
 8 2030   KARA   ON       67.0  8.47    2030
 9 2040   KARA   DJF     172.  12.7     2040
10 2040   KARA   JJAS     77.8 11.3     2040

Can someone provide some information on that.

Thank You

my_data %>%
  mutate(new_value_column = case_when(region == 'DJF'  ~ og_value_column - 10,
                                      region == 'MAM'  ~ og_value_column - 20,
                                      region == 'JJAS' ~ og_value_column - 30,                                     
                                      region == 'ON'   ~ og_value_column - 40,
  ))
1 Like

Thanks. It worked :slight_smile:

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.