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?