根据组的第一行变换组中的每一行

时间:2021-11-12 21:22:36

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:

我想将每行的val除以每组中第一行的val。我找到的唯一方法是引入一个新列:

 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?

有没有简单的方法来做到这一点?

1 个解决方案

#1


4  

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]

如果我们需要除以第一个'val'观察,只需要做val [1L]

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

#1


4  

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]

如果我们需要除以第一个'val'观察,只需要做val [1L]

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