I've been struggling with this for a while and could use some help.
I am trying to look at changes in consecutive dry days in a meteorological dataset. My dataframe has two columns, date and precipitation, and it looks something like this:
date <- seq(as.Date("2000-01-01"), as.Date("2000-01-30"), by="days")
precipitation <- c(1,0,0,0,5,4,7,0,5,0,0,0,0,0,0,8,4,0,0,2,0,0,2,4,5,1,1,0,0,3)
Data <- data.frame(date, precipitation)
I'd like to create a new column that shows the length of each stretch of consecutive days where precipitation==0. In other words, I would like the output to look something like this:
dry.streak <- c(0,3,0,0,0,0,0,1,0,6,0,0,0,0,0,0,0,2,0,0,2,0,0,0,0,0,0,2,0,0)
Data <- data.frame(date, precipitation, dry.streak)
where the value in dry.streak is the number of days that precipitation==0 starting from that point forward until it rains again (precipitation>0) but only for the first day in a consecutive series of dry days (the value should be zero, NA, or something else I can easily filter out for all days that aren't the first day of a dry streak so I can look at each streak independently when I run statistics on this).
So far, I've used dry.streak2 <- with(rle(as.logical(Data$precipitation==0)), lengths[values]) to create a separate vector of the length of each dry streak, but I don't know how to actually match this up with my dates which I'll need to do to look at changes in the length of dry streaks over time.
I'd appreciate any help you can give. Thank you!