I have a timeseries dataset in R with 17 time points. For each group of individuals (named ID), individuals were infected at the beginning of the time series (at timepoint 0). In some groups, individuals were re-infected one or more times throughout the time series.
I have a variable for days since first timepoint/ first infection, and another variable with days since previous check-up, and another variable denotes the timepoint of any infection after the first.
I would like to create a new variable with days since any infection. If there is no infection after the first, this should be the same as the column for days since first timepoint/infection. If there are infections after the first, this should be the summation of days since previous check-up, until the timepoint of the additional infection.
I think what I should be doing is taking infections occurring after timepoint 1, and adding together the days since previous check-up, until I experience another timepoint with infection.
I have tried multiple iterations of cumulative sums lagged for the rows of days since previous check up above until day 0 is reached, but I cannot figure out how to do this for groups with more than one infection.
Any help would be hugely appreciated.
ID: Individual ID for full timeseries timepoint: time point in time series status: Recruitment= time of first infection and beginning of time series. Follow up = all timepoints days_total_length: Number of days since first infection/ beginning of time series days_from_previous_round: number of days since last timepoint/ follow-up infection: point of (any) infection
library(tidyverse)
tibble::tribble(
~ID, ~timepoint, ~status, ~days_total_length, ~days_from_previous_round, ~infection,
"Sero-035", 0, "Recruitment", 0L, 0L, 1,
"Sero-035", 0.25, "Follow-up", 7L, 7L, NA,
"Sero-035", 1, "Follow-up", 31L, 24L, NA,
"Sero-035", 2, "Follow-up", 59L, 28L, NA,
"Sero-035", 3, "Follow-up", 90L, 31L, NA,
"Sero-035", 4, "Follow-up", 120L, 30L, NA,
"Sero-035", 5, "Follow-up", 151L, 31L, NA,
"Sero-035", 6, "Follow-up", 181L, 30L, NA,
"Sero-035", 7, "Follow-up", 211L, 30L, NA,
"Sero-035", 8, "Follow-up", 242L, 31L, NA,
"Sero-035", 9, "Follow-up", 272L, 30L, NA,
"Sero-035", 10, "Follow-up", 294L, 22L, NA,
"Sero-035", 11, "Follow-up", 334L, 40L, NA,
"Sero-035", 12, "Follow-up", 364L, 30L, NA,
"Sero-036", 0, "Recruitment", 0L, 0L, 1,
"Sero-036", 0.25, "Follow-up", 7L, 7L, NA,
"Sero-036", 1, "Follow-up", 31L, 24L, NA,
"Sero-036", 2, "Follow-up", 59L, 28L, NA,
"Sero-036", 3, "Follow-up", 91L, 32L, 1,
"Sero-036", 4, "Follow-up", 121L, 30L, NA,
"Sero-036", 5, "Follow-up", 154L, 33L, NA,
"Sero-036", 6, "Follow-up", 184L, 30L, NA,
"Sero-036", 7, "Follow-up", 214L, 30L, NA,
"Sero-036", 8, "Follow-up", 247L, 33L, NA,
"Sero-036", 9, "Follow-up", 278L, 31L, NA,
"Sero-036", 10, "Follow-up", 311L, 33L, NA,
"Sero-036", 11, "Follow-up", 341L, 30L, NA,
"Sero-036", 12, "Follow-up", 372L, 31L, 1,
"Sero-040", 0, "Recruitment", 0L, 0L, 1,
"Sero-040", 0.25, "Follow-up", 7L, 7L, NA,
"Sero-040", 1, "Follow-up", 31L, 24L, NA,
"Sero-040", 2, "Recurrence", 53L, 22L, 1,
"Sero-040", 3, "Follow-up", 84L, 31L, NA,
"Sero-040", 4, "Follow-up", 115L, 31L, NA,
"Sero-040", 5, "Follow-up", 149L, 34L, NA,
"Sero-040", 6, "Recurrence", 171L, 22L, 1,
"Sero-040", 7, "Follow-up", 196L, 25L, NA,
"Sero-040", 8, "Follow-up", 224L, 28L, NA,
"Sero-040", 9, "Follow-up", 260L, 36L, 1,
"Sero-040", 10, "Recurrence", 267L, 7L, 1,
"Sero-040", 11, "Follow-up", 291L, 24L, NA,
"Sero-040", 12, "Follow-up", 332L, 41L, 1,
"Sero-040", 13, "Follow-up", 366L, 34L, NA,
"Sero-041", 0, "Recruitment", 0L, 0L, 1,
"Sero-041", 0.25, "Follow-up", 6L, 6L, NA,
"Sero-041", 1, "Follow-up", 31L, 25L, NA,
"Sero-041", 2, "Recurrence", 52L, 21L, 1,
"Sero-041", 3, "Follow-up", 84L, 32L, NA,
"Sero-041", 4, "Recurrence", 104L, 20L, 1,
"Sero-041", 5, "Follow-up", 135L, 31L, NA,
"Sero-041", 6, "Follow-up", 168L, 33L, NA,
"Sero-041", 7, "Follow-up", 204L, 36L, NA,
"Sero-041", 8, "Follow-up", 237L, 33L, NA,
"Sero-041", 9, "Recurrence", 252L, 15L, 1,
"Sero-041", 10, "Recurrence", 274L, 22L, 1,
"Sero-041", 11, "Follow-up", 305L, 31L, NA,
"Sero-041", 12, "Recurrence", 329L, 24L, 1,
"Sero-041", 13, "Follow-up", 361L, 32L, NA,
"Sero-042", 0, "Recruitment", 0L, 0L, 1,
"Sero-042", 0.25, "Follow-up", 7L, 7L, NA,
"Sero-042", 1, "Follow-up", 32L, 25L, 1,
"Sero-042", 2, "Follow-up", 59L, 27L, NA,
"Sero-042", 3, "Follow-up", 101L, 42L, NA,
"Sero-042", 4, "Follow-up", 134L, 33L, NA,
"Sero-042", 5, "Follow-up", 168L, 34L, NA,
"Sero-042", 6, "Follow-up", 199L, 31L, 1,
"Sero-042", 7, "Follow-up", 234L, 35L, NA,
"Sero-042", 8, "Follow-up", 265L, 31L, 1,
"Sero-042", 9, "Follow-up", 289L, 24L, NA,
"Sero-042", 10, "Follow-up", 331L, 42L, NA,
"Sero-042", 11, "Follow-up", 364L, 33L, NA,
"Sero-042", 12, "Follow-up", 395L, 31L, NA,
"Sero-043", 0, "Recruitment", 0L, 0L, 1,
"Sero-043", 0.25, "Follow-up", 7L, 7L, NA,
"Sero-043", 1, "Follow-up", 31L, 24L, NA,
"Sero-043", 2, "Follow-up", 59L, 28L, NA,
"Sero-043", 3, "Follow-up", 90L, 31L, NA,
"Sero-043", 4, "Follow-up", 120L, 30L, NA,
"Sero-043", 5, "Follow-up", 151L, 31L, NA,
"Sero-043", 6, "Follow-up", 181L, 30L, NA,
"Sero-043", 7, "Follow-up", 211L, 30L, NA,
"Sero-043", 8, "Follow-up", 242L, 31L, NA,
"Sero-043", 9, "Follow-up", 272L, 30L, NA,
"Sero-043", 10, "Follow-up", 302L, 30L, NA,
"Sero-043", 11, "Follow-up", 332L, 30L, NA,
"Sero-043", 12, "Follow-up", 364L, 32L, NA,
"Sero-047", 0, "Recruitment", 0L, 0L, 1,
"Sero-047", 0.25, "Follow-up", 7L, 7L, NA,
"Sero-047", 1, "Follow-up", 33L, 26L, 1,
"Sero-047", 2, "Follow-up", 62L, 29L, NA,
"Sero-047", 3, "Follow-up", 98L, 36L, 1,
"Sero-047", 4, "Follow-up", 129L, 31L, 1,
"Sero-047", 5, "Follow-up", 161L, 32L, 1,
"Sero-047", 6, "Follow-up", 192L, 31L, NA,
"Sero-047", 7, "Follow-up", 232L, 40L, NA,
"Sero-047", 8, "Follow-up", 264L, 32L, NA,
"Sero-047", 9, "Follow-up", 297L, 33L, NA,
"Sero-047", 10, "Follow-up", 327L, 30L, NA,
"Sero-047", 11, "Follow-up", 360L, 33L, NA,
"Sero-047", 12, "Follow-up", 388L, 28L, NA
)