I need to count, from the total period that each person was in the study, how much time they contributed by age-interval. Age is represented in days. For example, the first participant started at age 1306 and ended in 8509, for the first interval he contributed 520 days (1826-1306), for the second interval he contributed 1460 days (3287 - 1827), and so on until the age 14.999 years. More specifically, I want to split the total duration up to 14.9999 years into four variables, each containing the total duration for each interval.
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")