我想计算数据框每一列每年的最大连续行数 = 0。 我尝试了这个,但它分别适用于每一列:
library(data.table)
data <- data.frame(YEAR = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4),
PREC1 = c(0, 0, 0, 1, 1, 4, 1, 0, 0, 4, 2, 1, 0, 0, 0, 0, 5, 2, 8, 0),
PREC2 = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 8, 0, 1),
PREC3 = c(0, 0, 12, 1, 3, 0, 0, 0, 0, 2, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0),
PREC4 = c(0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 8, 0, 0, 9, 8, 0, 0),
PREC5 = c(0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 7, 1, 0, 1),
PREC6 = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 9, 6, 9, 0),
PREC7 = c(1, 0, 0, 0, 10, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 4, 2, 4, 1),
PREC8 = c(0, 0, 0, 1, 8, 0, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0, 0, 9, 0, 0),
PREC9 = c(0, 0, 0, 1, 5, 0, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0, 0, 5, 0, 0),
PREC10 = c(0, 0, 0, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0, 0, 4, 0, 0),
PREC11 = c(0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0, 0, 7, 2, 0),
PREC12 = c(0, 0, 0, 1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 9, 2, 0))
df=data
setDT(df)
df[, Consec_Days := seq_len(.N),rleid(PREC1 == 0)]
df[PREC1 != 0, Consec_Days := NA]
df=na.omit(df)
head(df)
library(plyr)
CDD = ddply(df, .(YEAR), summarize,
Prec = max(Consec_Days));CDD
如何同时对数据框的所有列执行此操作?
带有
dplyr
的一个选项可能是:
data %>%
summarise(across(starts_with("PREC"), ~ with(rle(. == 0), max(lengths[values]))),
.by = YEAR)
YEAR PREC1 PREC2 PREC3 PREC4 PREC5 PREC6 PREC7 PREC8 PREC9 PREC10 PREC11 PREC12
1 1 3 4 2 4 4 4 3 3 3 3 3 3
2 2 2 4 4 4 4 4 4 4 4 4 4 5
3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 1 2 5 2 1 1 1 2 2 2 2 2