I'm trying to count the number of precipitation below a certain threshold (let's say less or equal than 50) between two dates.
Basically, I have a vector
cuts
cuts
set.seed(12345)
df = data.frame(date = seq(as.Date("2000/03/01"), as.Date("2002/03/01"), "days"),
precipitation = rnorm(length(seq(as.Date("2000/03/01"), as.Date("2002/03/01"), "days")),80,20))
cuts = c("2001-11-25","2002-01-01","2002-02-18","2002-03-01")
for (i in 1:length(cuts)) {
df %>% summarise(count.prec = if (date > cuts[i] | date < cuts[i+1]) {count(precipitation <= 50)})
}
Error: no applicable method for 'group_by_' applied to an object of class "logical"
In addition: Warning message:
In if (c(11017, 11018, 11019, 11020, 11021, 11022, 11023, 11024, :
the condition has length > 1 and only the first element will be used
for (i in 1:length(cuts)) {
df %>% if (date > cuts[i] | date < cuts[i+1])%>% summarise(count.prec = count(precipitation <= 50))
}
You could try:
df %>%
group_by(gr = cut(date, breaks = as.Date(cuts))) %>%
summarise(res = sum(precipitation <= 50))
Which gives:
# A tibble: 4 × 2
gr res
<fctr> <int>
1 2001-11-25 1
2 2002-01-01 4
3 2002-02-18 2
4 NA 40