mardi 21 avril 2020

How can I conditionally remove data values the day AFTER a condition is met within an R dataframe?

Here is a sample dataframe:

date <- rep(seq(as.Date("2018-1-1"), as.Date("2018-1-4"), by = 
"days"),each=5)
plot <- rep(c(1:4), each = 5)
treatment <- rep(c('control','treat'),each = 10)
rain <- c(0,0,2,0,1,0,0,0,0,0,5,0,0,0,2.2,0,0,0.9,0,0)
Value <- (seq(1:20))

df <- data.frame(date, plot, treatment, rain,value)

What I'm working with is some environmental data that includes daily rainfall totals. I want to remove all 'values' (mark as 'NA') for all days in which the rain total exceeds zero (rain >0) and for all days after a rain event. For example, if rain >0 on 1/1/2018, marked all data in the 'value' coulmn as 'NA' corresponding to 1/1/2018 and 1/2/2018.To do the conditional for the same day is easy enough, but I'm scratching my head on how to apply this to the day after.

Thank you.

Aucun commentaire:

Enregistrer un commentaire