I am working on a temperature data set and I am looking to highlight days of consecutive max temperatures of over 30C and create a new TRUE/FALSE column in a data frame that highlights a heat wave. To do this I need to look at the previous 3 days data and verify whether they all meet this condition, as well as the current day itself.
I have tried to use the lag command to generate multiple new columns for -1 days, -2 days and -3 days and then use an ifelse statement to verify all columns are true. But this doesn't seem like an efficient way of doing things and I can't seem to get the lag variable to look in the past. Since multiple stations record daily data I need to also look at each weather station individually to verify a heat wave on a given day.
station_id = c(rep(1,10), rep(2,10))
date = c("20190129", "20190130", "20190131", "20190201", "20190202", "20190203", "20190204", "20190205", "20190206", "20190207")
max_temp = c(27, 35, 36, 38, 31, 32, 33, 29, 28, 26, 21, 20, 13, 31, 31, 32, 33, 35, 25, 20)
data = data.frame(station_id, date, max_temp)
print(data)
lag(data$max_temp, 3) #gives the forward lagging but i cant seem to be able to look backwards using negative numbers, perhaps I need to not include the first few columns?