我需要根据每个人的研究总时间来计算他们按年龄间隔贡献的时间。年龄以天为单位。例如,第一个参与者开始于1306年,结束于8509,在第一个间隔中,他贡献了520天(1826-1306),在第二个间隔中,他贡献了1460天(3287-1827),依此类推,直到14.999岁年。
library(tidyverse)
Age4 <- round(4.999*365.25)
Age8 <- round(8.999*365.25)
Age12 <- round(12.999*365.25)
Age14 <- round(14.999*365.25)
df <- tibble(code = 1:15, start_age = round(rnorm(15, 2000, 1500)), end_age = round(rnorm(15, 6000, 2000)),
mo_dur = end_age - start_age,
Age4 , # from 0 to 4.99,
Age8 , # from 5 to 8.999
Age12, # from 9 to 12.999
Age14) # from 13 to 14.999
code start_age end_age mo_dur Age4 Age8 Age12 Age14
1 1306 8509 7203 1826 3287 4748 5478
2 2007 3743 1736 1826 3287 4748 5478
3 4176 9119 4943 1826 3287 4748 5478
4 3129 7416 4287 1826 3287 4748 5478
5 3449 7869 4420 1826 3287 4748 5478
6 2703 7367 4664 1826 3287 4748 5478
7 1639 8038 6399 1826 3287 4748 5478
8 3549 5519 1970 1826 3287 4748 5478
9 1040 9355 8315 1826 3287 4748 5478
10 26 6818 6792 1826 3287 4748 5478
11 2543 4223 1680 1826 3287 4748 5478
12 3082 4602 1520 1826 3287 4748 5478
13 5728 7040 1312 1826 3287 4748 5478
14 522 8314 7792 1826 3287 4748 5478
15 1492 5779 4287 1826 3287 4748 5478
ggplot(df) +
geom_segment(aes(x = start_age/365.25, xend = end_age/365.25,
y = code, yend = code ),
arrow = arrow(length = unit(0.03, "npc"))) +
geom_point(aes(start_age/365.25, code)) +
geom_text(vjust = -0.5, hjust=0, size = 3,
aes(x = start_age/365.25, y = code,
label = paste(round(mo_dur/365.25, 2), "Total duration"))) +
geom_vline(xintercept = Age4/365.25, color = "red") + geom_vline(xintercept = Age8/365.25, color = "red") +
geom_vline(xintercept = Age12/365.25, color = "red") + geom_vline(xintercept = Age14/365.25, color = "red")
鉴于问题的描述,以下两个解决方案是等效的。
df %>%
mutate(Count = Age4 - start_age,
Count = Count + Age8 - (Age4 + 1),
Count = Count + Age12 - (Age8 + 1),
Count = Count + Age14 - (Age12 + 1))
df %>%
mutate(Count = Age14 - start_age - 3)