Mutate each line in the group according to the first line of the group

For example, I have a data frame:

df <- data.frame(grp = c(1,1,1,1,1,2,2,2,2,2), idx = c(1,2,3,4,5,1,2,3,4,5), val = c(4,6,1,7,2,8,5,3,9,1)) 

I want to split the val of each line by the val of the first line in each group. The only way I found is to enter a new column:

  df %>% group_by(grp) %>% arrange(idx) %>% mutate(t = ifelse(row_number(idx) == 1, val, 0)) %>% mutate(val = val / sum(t)) 

Is there an easy way to do this?

+5
source share
1 answer

We can do it

  df %>% group_by(grp) %>% arrange(idx) %>% mutate(val = val/sum((row_number() == 1)*val)) # A tibble: 10 x 3 # Groups: grp [2] # grp idx val # <dbl> <dbl> <dbl> # 1 1 1 1.000 # 2 2 1 1.000 # 3 1 2 1.500 # 4 2 2 0.625 # 5 1 3 0.250 # 6 2 3 0.375 # 7 1 4 1.750 # 8 2 4 1.125 # 9 1 5 0.500 #10 2 5 0.125 

If we need to split the first observation "val", just do val[1L]

 df %>% group_by(grp) %>% arrange(idx) %>% mtuate(val = val/val[1L]) 
+4
source

Source: https://habr.com/ru/post/1269246/


All Articles