我有一个包含三个日期列和一个数字列的数据框。第一个是客户开始购买,第二个是购买的到期日,第三个是它的价值,最后一个列是客户付款的时间。
我想总结一下购买的价值,按日期计算,公开购买的数量 - 这意味着不仅没有付款而且还少于到期时间。数据框如下:
set.seed(3)
open <- as.Date(rep(c("2017-03-22","2017-03-23","2017-03-24","2017-03-25"), 3), format = "%Y-%m-%d") #Purchase's open date
due <- open + 3 #Due date's purchase
purchase <- purchase <- round(runif(12, 50,150),2) #Quando ele gastou
paid <- as.Date(c("2017-03-22",NA,"2017-03-24","2017-03-25",NA,"2017-03-23",NA,"2017-03-25",NA,"2017-03-23",NA,NA)) #Value of purchase
base <- data.frame(open,due,purchase,paid)
答案 0 :(得分:2)
像这样:
library(dplyr)
today <- Sys.Date()
base %>%
group_by(open) %>%
summarize(value_open = sum(purchase[today < due || is.na(paid)]))