Home > OS >  Function for measuring change compared to year zero
Function for measuring change compared to year zero

Time:10-09

I have a data frame in R that has a column for "low", "mid", and "high" and a column for year (either 2019, 2020, or 2021) with averages following. I want to find change by level compared to the first year (2019) (aka 2020-2019 and 2021-2020). I was using the function: df %>% group_by(level) %>% arrange(year) %>% mutate(growth_rate = average / lag(average)). However, using this function, 2021 is compared to 2020. Is there an easy way to do this that I am missing?

level year average
Low 2019 1300
Mid 2019 2065
High 2019 2194
Low 2020 2966
Mid 2020 4723
High 2020 5915
Low 2021 4049
Mid 2021 5915
High 2021 5978

CodePudding user response:

Try this:

df %>%
   group_by(level) %>% 
   arrange(year) %>% 
   mutate(growth_rate = average / first(average))

Also, can you type dput(<your data here>) in the console and paste that into your question? It will help others to copy and paste the data frame you're working with.

CodePudding user response:

We may do this directly with which.min i.e. after grouping by 'level', divide the 'average' by the 'average' value on the minimum 'year'

library(dplyr)
df %>%
    group_by(level) %>%
    mutate(growth_rate = average/average[which.min(year)])
  •  Tags:  
  • r
  • Related