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
(Up front: all of these solutions rely on the ordering being set ahead of time. It's not hard to sort externally, perhaps with dplyr::arrange
or base order
.)
With dplyr
this can be done with:
library(dplyr)
df %>%
group_by(plot, treatment) %>%
mutate(value = if_else(rain > 0 | lag(rain > 0, default = FALSE), NA_integer_, value)) %>%
ungroup()
# # A tibble: 20 x 5
# date plot treatment rain value
# <chr> <int> <chr> <int> <int>
# 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
Alternatively, with data.table
library(data.table)
DT <- as.data.table(df)
DT[rain > 0 | shift(rain > 0), value := NA, by = .(plot, treatment)]
And perhaps less-elegantly in base-R:
do.call(rbind.data.frame,
by(df, df[,c("plot", "treatment")], function(x) {
n <- nrow(x)
within(x, { value[ rain > 0 | c(FALSE, rain[-n] > 0) ] = NA })
})
)
(though this last might not keep the order of rows the same).
Data:
df <- read.table(header=TRUE, stringsAsFactors=FALSE, text="
date plot treatment rain value
2018-01-01 1 control 0 1
2018-01-02 1 control 0 2
2018-01-03 1 control 2 3
2018-01-04 1 control 0 4
2018-01-05 1 control 0 5
2018-01-01 2 control 0 6
2018-01-02 2 control 0 7
2018-01-03 2 control 2 8
2018-01-04 2 control 0 9
2018-01-05 2 control 0 10
2018-01-01 3 treat 0 11
2018-01-02 3 treat 0 12
2018-01-03 3 treat 2 13
2018-01-04 3 treat 0 14
2018-01-05 3 treat 0 15
2018-01-01 4 treat 0 16
2018-01-02 4 treat 0 17
2018-01-03 4 treat 2 18
2018-01-04 4 treat 0 19
2018-01-05 4 treat 0 20")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.