I’m working with longitudinal dataset, in which i recorded an experiment for 72 hours. The first 24 hours were a baseline recording, after 24 hours I started experimental conditions. The simplified dataframe looks something like this:
df <- data.frame(
accumulated_hours = rep(0:71, each = 1),
ID = rep(c('A', 'B'), each = 72),
o2 = runif(144, 5, 15) # Random o2 values for example
)
I would like to calculate the ratio of o2, by dividing the value per hour with the corresponding baseline hour. So the o2 value of hour 24 and 48 divided by the o2 of hour 0, 25 and 49 by o2 value of hour 1 etc. How can I best approach this?
I have tried solving it by case_when, but I cannot get it to work and it seems not the most efficient form if I would have to rewrite this for 20 variables.
df %>%
group_by(ID) %>%
mutate(
ratio = case_when(
accumulated_hours >= 24 & accumulated_hours < 48 ~ (o2 / o2[accumulated_hours - 24]),
accumulated_hours >= 48 & accumulated_hours < 72 ~ (o2 / o2[accumulated_hours - 48]),
TRUE ~ NA_real_
)
)
I do have timestamps as well from which i derived the accumulated hours (mdy_hms), if it is more convenient to use.
Many thanks in advance for any suggestions!