【发布时间】:2017-01-19 02:49:34
【问题描述】:
我正在尝试计算两个日期之间低于某个阈值(比如说小于或等于 50)的降水量。
基本上,我有一个向量cuts,其中包含我想要计算的日期。我想使用cuts 向量将数据集“子集”到不同的 bin 中,然后计算下雨少于 50 毫米的事件的数量。
我目前正在使用 dplyr 和 for 循环,但没有任何效果。
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))
}
【问题讨论】: