用
data.table
:
# set seed for reproducibility
set.seed(1)
# data frame
df <- data.frame(Date = sample(seq(as.Date("2019-01-01"), as.Date("2019-01-09"), by = "days"), 30, replace = T),
Min = sample(c(0:5), 30, replace = T), stringsAsFactors = F)
# load packages
library(magrittr)
library(data.table)
# make df into data.table
setDT(df)
# establish which Date values have Min = 0
minVals <- df[Min == 0, unique(Date)]
# Count date and set those rows with Date Min = 0 to 0
res <- df[, .N, by = 'Date'][
Date %in% minVals, N := 0
]
结果:
> res
Date N
1: 2019-01-03 0
2: 2019-01-04 0
3: 2019-01-06 0
4: 2019-01-09 5
5: 2019-01-02 5
6: 2019-01-01 2
7: 2019-01-07 0
8: 2019-01-05 1
9: 2019-01-08 1
如果你能以一种我们可以在提供答案时实际测试的方式发布一小段数据,那就太好了。尝试
dput(head(df, 10))
,
R
将在控制台上显示一个输出,该输出应该是构建实际数据片段的一段代码。
一
dplyr
解决方案:
library(dplyr)
count(df, Date) %>%
mutate(n = ifelse(Date %in% pull(filter(df, Min == 0), Date), 0, n))
导致:
# A tibble: 9 x 2
Date n
<date> <dbl>
1 2019-01-01 2
2 2019-01-02 5
3 2019-01-03 0
4 2019-01-04 0
5 2019-01-05 1
6 2019-01-06 0
7 2019-01-07 0
8 2019-01-08 1
9 2019-01-09 5