jeudi 23 avril 2020

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

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/3/2018, marked all data in the 'value' coulmn as 'NA' corresponding to 1/3/2018 and 1/4/2018.To do the conditional for the same day is easy enough, but I'm scratching my head on how to reference the following day as well.

Here is a sample dataframe:

date <- seq(as.Date("2018-1-1"), as.Date("2018-1-5"), by = 
              "1 days")
plot <- rep(c(1:4), each = 5)
treatment <- rep(c('control','treat'),each = 10)
rain <- c(0,0,2,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2,0,0)
value <- (seq(1:20))
df <- data.frame(date, plot, treatment, rain,value)
df$date <- rep(date, length=nrow(df))
df



date plot treatment rain value
1  2018-01-01    1   control    0     1
2  2018-01-02    1   control    0     2
3  2018-01-03    1   control    2     3
4  2018-01-04    1   control    0     4
5  2018-01-05    1   control    0     5
6  2018-01-01    2   control    0     6
7  2018-01-02    2   control    0     7
8  2018-01-03    2   control    2     8
9  2018-01-04    2   control    0     9
10 2018-01-05    2   control    0    10
11 2018-01-01    3     treat    0    11
12 2018-01-02    3     treat    0    12
13 2018-01-03    3     treat    2    13
14 2018-01-04    3     treat    0    14
15 2018-01-05    3     treat    0    15
16 2018-01-01    4     treat    0    16
17 2018-01-02    4     treat    0    17
18 2018-01-03    4     treat    2    18
19 2018-01-04    4     treat    0    19
20 2018-01-05    4     treat    0    20

Desired result:

            date plot treatment rain value
1  2018-01-01    1   control    0     1
2  2018-01-02    1   control    0     2
3  2018-01-03    1   control    2    NA
4  2018-01-04    1   control    0    NA
5  2018-01-05    1   control    0     5
6  2018-01-01    2   control    0     6
7  2018-01-02    2   control    0     7
8  2018-01-03    2   control    2    NA
9  2018-01-04    2   control    0    NA
10 2018-01-05    2   control    0    10
11 2018-01-01    3     treat    0    11
12 2018-01-02    3     treat    0    12
13 2018-01-03    3     treat    2    NA
14 2018-01-04    3     treat    0    NA
15 2018-01-05    3     treat    0    15
16 2018-01-01    4     treat    0    16
17 2018-01-02    4     treat    0    17
18 2018-01-03    4     treat    2    NA
19 2018-01-04    4     treat    0    NA
20 2018-01-05    4     treat    0    20

Aucun commentaire:

Enregistrer un commentaire