I have a database where individuals were conditioned using different types of event, and I would like to assess whether the proportion of high intensity events over the 7 days preceding the current event, impacted how they respond in the current event.
Rows in the database are events, and each event has an individual ID variable (ID.2), and an intensity categorization (Intensity.code, low/medium/high).
I already have a code where I can count the number of events per individual in the past week (where APW is events per week):
ACdatas <- ACdatas %>%
group_by(ID.2) %>%
mutate(
APW = sapply(Datetime, function(d) sum(between(as.numeric(difftime(d, Datetime[Datetime < d], units = "days")), 0, 7)))
) %>% ungroup()
Now I would like to calculate for each individual, within the past week, what proportion of the total events were high intensity events.
Here is a subset of the pertinent fields in my dataset (there are 50 individuals and 5900-ish records in the actual dataset):
head(ACdatas)
> Datetime ID.2 Intensity.code
> 1 2019-05-25 11:57 139 Medium
> 2 2019-05-20 19:42 139 High
> 3 2019-05-21 20:12 139 High
> 4 2019-05-26 17:27 139 Low
> 5 2019-05-23 9:13 139 Low
> 6 2019-05-22 16:18 139 Medium
> 7 2019-06-10 17:27 152 Low
> 8 2019-06-09 9:13 152 Low
> 9 2019-06-08 16:18 152 Low
> 10 2019-06-08 17:00 152 High
I've tried to add a proportion calculation to the mutate function used to calculate number of action in a week (above), but haven't had any luck yet! Any help would be so very appreciated!
a wonderful friend solved this for me, and I thought I'd post the answer here in case can help someone else down the line! First, calculate number of actions in the last week
Then calculate high intensity actions in the last week
Then calculate proportion that were high intensity in the past week