I need to create a new column that tells me if a process that started on a given date encountered a temperature below zero. I'd like to iterate over every row of the dataframe, checking a group of rows that begins with the current row and includes the next x number of rows, where x is the value in the 'duration' column. If any of the temp values in that group of rows is below zero, then the value in the new column would be TRUE.
set.seed(1)
df = data.frame(date = seq(as.Date("2020-01-01"),as.Date("2020-04-09"), by=1),
temp = runif(100,min=-2,max=30),
duration = sample.int(20,100,replace=TRUE))
> head(df, n=10)
date temp duration
1 2020-01-01 6.49627722 12
2 2020-01-02 9.90796479 16
3 2020-01-03 16.33130763 1
4 2020-01-04 27.06264928 13
5 2020-01-05 4.45382179 6
6 2020-01-06 26.74846992 17
7 2020-01-07 28.22960860 9
8 2020-01-08 19.14552936 7
9 2020-01-09 18.13164940 19
10 2020-01-10 -0.02283935 18
For each row I'm trying to answer the question "Did the process that started on this date and lasted [duration] days encounter a temperature below zero?" Since the value of 'duration' in the first row is 12, and there is a negative number in the tenth row of the 'temp' column, then the value of the new column would be TRUE. The second row would also be TRUE, the third row would be FALSE, and the following six rows would be TRUE.
I've been playing with the map_lgl function of purrr, but I'm not getting anywhere.