从 R 中的 table 获取累积死亡率计数数据
Getting cumulative mortality count data from a table in R
我每年有 table 不同年龄的死亡人数,格式如下:
Year Age Deaths
1923 1 30
1923 2 22
1923 3 10
1923 4 12
1923 5 4
1923 1 40
1924 2 11
1924 3 10
1924 4 3
1924 5 5
1924 1 40
1924 2 24
1924 3 32
1924 4 12
1924 5 3
每年的年龄范围实际上是 100,年份是 2020 年,但我缩短了 table 作为示例。我要回答的问题是:'At what age are X% of those born in 1923 no longer living?'我知道Y人是1923年出生的。
因此,为了跟踪这个队列,我想通读 table 并找出 1924 年有多少 1 岁儿童死亡,1925 年有多少 2 岁儿童死亡等,当我找到 [=20] 的年份时停止=]X% 的 Y 出生于 1923 年的人已经不在人世。
有没有人对我在寻找每年时如何在 R 中进行这样的累积计数有任何建议,并且在计算时,每年我想要的行的年龄应该增加 +1。我会对 tidyverse 的答案特别感兴趣(我想他们会涉及使用 Purr?),因为这是我一直在努力学习的,但任何建议都会受到赞赏。
在此先感谢您的帮助和建议。
或许,我们可以做一个累加和的分组
library(data.table)
setDT(df)[, CumDeath := cumsum(Deaths), .(BirthYear = Year - Age)][]
数据
df <- structure(list(Year = c(1923, 1923, 1923, 1923, 1923, 1924, 1924,
1924, 1924, 1924, 1925, 1925, 1925, 1925, 1925), Age = c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Deaths = c(31L,
15L, 14L, 3L, 42L, 50L, 43L, 37L, 14L, 25L, 26L, 27L, 5L, 27L,
28L)), class = "data.frame", row.names = c(NA, -15L))
听起来您可能想先确定队列,哪个出生年份可以代表。然后,您可以 group_by
出生年份并计算累计死亡人数。我为此示例编写了一些示例数据。
library(tidyverse)
df %>%
mutate(BirthYear = Year - Age) %>%
group_by(BirthYear) %>%
mutate(CumDeath = cumsum(Deaths)) %>%
arrange(BirthYear)
输出
Year Age Deaths BirthYear CumDeath
<dbl> <int> <int> <dbl> <int>
1 1923 5 42 1918 42
2 1923 4 3 1919 3
3 1924 5 25 1919 28
4 1923 3 14 1920 14
5 1924 4 14 1920 28
6 1925 5 28 1920 56
7 1923 2 15 1921 15
8 1924 3 37 1921 52
9 1925 4 27 1921 79
10 1923 1 31 1922 31
11 1924 2 43 1922 74
12 1925 3 5 1922 79
13 1924 1 50 1923 50
14 1925 2 27 1923 77
15 1925 1 26 1924 26
数据
df <- structure(list(Year = c(1923, 1923, 1923, 1923, 1923, 1924, 1924,
1924, 1924, 1924, 1925, 1925, 1925, 1925, 1925), Age = c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Deaths = c(31L,
15L, 14L, 3L, 42L, 50L, 43L, 37L, 14L, 25L, 26L, 27L, 5L, 27L,
28L)), class = "data.frame", row.names = c(NA, -15L))
我每年有 table 不同年龄的死亡人数,格式如下:
Year Age Deaths
1923 1 30
1923 2 22
1923 3 10
1923 4 12
1923 5 4
1923 1 40
1924 2 11
1924 3 10
1924 4 3
1924 5 5
1924 1 40
1924 2 24
1924 3 32
1924 4 12
1924 5 3
每年的年龄范围实际上是 100,年份是 2020 年,但我缩短了 table 作为示例。我要回答的问题是:'At what age are X% of those born in 1923 no longer living?'我知道Y人是1923年出生的。 因此,为了跟踪这个队列,我想通读 table 并找出 1924 年有多少 1 岁儿童死亡,1925 年有多少 2 岁儿童死亡等,当我找到 [=20] 的年份时停止=]X% 的 Y 出生于 1923 年的人已经不在人世。
有没有人对我在寻找每年时如何在 R 中进行这样的累积计数有任何建议,并且在计算时,每年我想要的行的年龄应该增加 +1。我会对 tidyverse 的答案特别感兴趣(我想他们会涉及使用 Purr?),因为这是我一直在努力学习的,但任何建议都会受到赞赏。
在此先感谢您的帮助和建议。
或许,我们可以做一个累加和的分组
library(data.table)
setDT(df)[, CumDeath := cumsum(Deaths), .(BirthYear = Year - Age)][]
数据
df <- structure(list(Year = c(1923, 1923, 1923, 1923, 1923, 1924, 1924,
1924, 1924, 1924, 1925, 1925, 1925, 1925, 1925), Age = c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Deaths = c(31L,
15L, 14L, 3L, 42L, 50L, 43L, 37L, 14L, 25L, 26L, 27L, 5L, 27L,
28L)), class = "data.frame", row.names = c(NA, -15L))
听起来您可能想先确定队列,哪个出生年份可以代表。然后,您可以 group_by
出生年份并计算累计死亡人数。我为此示例编写了一些示例数据。
library(tidyverse)
df %>%
mutate(BirthYear = Year - Age) %>%
group_by(BirthYear) %>%
mutate(CumDeath = cumsum(Deaths)) %>%
arrange(BirthYear)
输出
Year Age Deaths BirthYear CumDeath
<dbl> <int> <int> <dbl> <int>
1 1923 5 42 1918 42
2 1923 4 3 1919 3
3 1924 5 25 1919 28
4 1923 3 14 1920 14
5 1924 4 14 1920 28
6 1925 5 28 1920 56
7 1923 2 15 1921 15
8 1924 3 37 1921 52
9 1925 4 27 1921 79
10 1923 1 31 1922 31
11 1924 2 43 1922 74
12 1925 3 5 1922 79
13 1924 1 50 1923 50
14 1925 2 27 1923 77
15 1925 1 26 1924 26
数据
df <- structure(list(Year = c(1923, 1923, 1923, 1923, 1923, 1924, 1924,
1924, 1924, 1924, 1925, 1925, 1925, 1925, 1925), Age = c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Deaths = c(31L,
15L, 14L, 3L, 42L, 50L, 43L, 37L, 14L, 25L, 26L, 27L, 5L, 27L,
28L)), class = "data.frame", row.names = c(NA, -15L))