如何通过特定 ID 撤消 R 中的累积值?
How to undo accumulated values in R by specifc IDs?
我有一个数据集,其中每一行包含女性每年累计的后代数量。有些人从一年到下一年都死了。
Year ID Sex AccumulatedFecundity
1 2411 female 29
1 583 female 30
1 469 female 147
2 2290 female 161
2 583 female 169
2 788 female 27
我想撤消这个积累并获得原始值。在我的示例中,女性 583 的第二次出现应获得原始值 139 (169-30)。
我们可以按 'ID' 和 'Sex'
分组 diff
library(dplyr)
df1 %>%
group_by(ID, Sex) %>%
mutate(old = c(first(AccumulatedFecundity), diff(AccumulatedFecundity))) %>%
ungroup
-输出
# A tibble: 6 x 5
# Year ID Sex AccumulatedFecundity old
# <int> <int> <chr> <int> <int>
#1 1 2411 female 29 29
#2 1 583 female 30 30
#3 1 469 female 147 147
#4 2 2290 female 161 161
#5 2 583 female 169 139
#6 2 788 female 27 27
数据
df1 <- structure(list(Year = c(1L, 1L, 1L, 2L, 2L, 2L), ID = c(2411L,
583L, 469L, 2290L, 583L, 788L), Sex = c("female", "female", "female",
"female", "female", "female"), AccumulatedFecundity = c(29L,
30L, 147L, 161L, 169L, 27L)), class = "data.frame", row.names = c(NA,
-6L))
我有一个数据集,其中每一行包含女性每年累计的后代数量。有些人从一年到下一年都死了。
Year ID Sex AccumulatedFecundity
1 2411 female 29
1 583 female 30
1 469 female 147
2 2290 female 161
2 583 female 169
2 788 female 27
我想撤消这个积累并获得原始值。在我的示例中,女性 583 的第二次出现应获得原始值 139 (169-30)。
我们可以按 'ID' 和 'Sex'
分组diff
library(dplyr)
df1 %>%
group_by(ID, Sex) %>%
mutate(old = c(first(AccumulatedFecundity), diff(AccumulatedFecundity))) %>%
ungroup
-输出
# A tibble: 6 x 5
# Year ID Sex AccumulatedFecundity old
# <int> <int> <chr> <int> <int>
#1 1 2411 female 29 29
#2 1 583 female 30 30
#3 1 469 female 147 147
#4 2 2290 female 161 161
#5 2 583 female 169 139
#6 2 788 female 27 27
数据
df1 <- structure(list(Year = c(1L, 1L, 1L, 2L, 2L, 2L), ID = c(2411L,
583L, 469L, 2290L, 583L, 788L), Sex = c("female", "female", "female",
"female", "female", "female"), AccumulatedFecundity = c(29L,
30L, 147L, 161L, 169L, 27L)), class = "data.frame", row.names = c(NA,
-6L))