Home > Blockchain >  Create relative value from the first row of an grouped dataframe
Create relative value from the first row of an grouped dataframe

Time:11-23

I have

df<-data.frame(year=c(2010, 2010, 2010, 2011, 2011), day=c(1,2,3,1,2), value=c(5,6,7,8,5))

year day value
1 2010   1     5
2 2010   2     6
3 2010   3     7
4 2011   1     8
5 2011   2     5

I would like create a variable with the relative change of value within each year (relative to the first day of the year) and end up with

  year day value rel.value
1 2010   1     5     1.000
2 2010   2     6     1.200
3 2010   3     7     1.400
4 2011   1     8     1.000
5 2011   2     5     0.625

is it possible to do this within the dplyr framework, without looking up to a second table?

I've used slice to obtain a table with the values of day 1 of each year but I don't know how to divide all subsequent values of the same year.

CodePudding user response:

After grouping by 'year', divide the value by the first element of 'value' to create the column

library(dplyr)
df <- df %>% 
  group_by(year) %>%
  mutate(rel.value = value/first(value)) %>% 
  ungroup

-output

df
# A tibble: 5 × 4
   year   day value rel.value
  <dbl> <dbl> <dbl>     <dbl>
1  2010     1     5     1    
2  2010     2     6     1.2  
3  2010     3     7     1.4  
4  2011     1     8     1    
5  2011     2     5     0.625
  • Related