Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R groupby and divide column by a value in column

I have a dataframe like so:

id  year  month  val
1   2020  1      50
1   2020  7      80
1   2021  1      40
1   2021  7      70
.
.

Now, I want to index all the values using Jan 2020 as index year for each id. Essentially group by id, then divide val with val at Jan 2020 * 100. So the final dataframe would look something like this:

id  year  month  val
1   2020  1      100
1   2020  7      160
1   2021  1      80
1   2021  7      140
.
.

This is what I tried till now:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df %>% group_by(id) %>% mutate(val = 100*val/[val at Jan 2020])

I can separately get val at Jan 2020 like so:

df %>% filter(year==2020, month==1) %>% select(val)

But it doesn’t work together:

df %>% group_by(id) %>% mutate(val = 100*val/(df %>% filter(year==2020, month==1) %>% select(val)))

The above throws error

>Solution :

A dplyr approach

library(dplyr)

df %>% 
  group_by(id) %>%
  mutate(val = val / val[year == 2020 & month == 1] * 100) %>%
  ungroup()
# A tibble: 4 × 4
     id  year month   val
  <int> <int> <int> <dbl>
1     1  2020     1   100
2     1  2020     7   160
3     1  2021     1    80
4     1  2021     7   140
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading