Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dplyr

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 divide the val of each row by the val of the first row in each group. The only way I found is to introduce 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 any simple way to do this?

like image 570
Zephyr Dodolee Avatar asked Jun 27 '17 07:06

Zephyr Dodolee


1 Answers

We can do

 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 divide by the first 'val' observation, just do val[1L]

df %>%
   group_by(grp) %>%
   arrange(idx) %>%
   mtuate(val = val/val[1L])
like image 117
akrun Avatar answered Sep 17 '22 20:09

akrun