R-使用开始和结束日期计算一段时间内的项目计数 [英] R- Calculate a count of items over time using start and end dates
问题描述
我想使用开始和结束日期计算一段时间内的项目数量。
I want to calculate a count of items over time using their Start and End dates.
某些示例数据
START <- as.Date(c("2014-01-01", "2014-01-02","2014-01-03","2014-01-03"))
END <- as.Date(c("2014-01-04", "2014-01-03","2014-01-03","2014-01-04"))
df <- data.frame(START,END)
df
给
START END
1 2014-01-01 2014-01-04
2 2014-01-02 2014-01-03
3 2014-01-03 2014-01-03
4 2014-01-03 2014-01-04
表显示这些项目的时间(根据其开始和结束时间)的计数如下所示:
A table showing a count of these items across time (based on their Start and End times) is as follows:
DATETIME COUNT
2014-01-01 1
2014-01-02 2
2014-01-03 4
2014-01-04 2
可以使用R来完成,特别是使用dplyr?非常感谢。
Can this be done using R, especially using dplyr? Many thanks.
推荐答案
这样做。您可以根据需要更改列名。
This would do it. You can change the column names as necessary.
as.data.frame(table(Reduce(c, Map(seq, df$START, df$END, by = 1))))
# Var1 Freq
# 1 2014-01-01 1
# 2 2014-01-02 2
# 3 2014-01-03 4
# 4 2014-01-04 2
如前所述在评论中,上述解决方案中的 Var1
现在是一个因素,而不是日期。要将日期类保留在第一列中,您可以对上述解决方案进行更多工作,或者使用 plyr :: count
而不是作为。 data.frame(table(...))
As noted in the comments, Var1
in the above solution is now a factor, and not a date. To keep the date class in the first column, you could do some more work to the above solution, or use plyr::count
instead of as.data.frame(table(...))
library(plyr)
count(Reduce(c, Map(seq, df$START, df$END, by = 1)))
# x freq
# 1 2014-01-01 1
# 2 2014-01-02 2
# 3 2014-01-03 4
# 4 2014-01-04 2
这篇关于R-使用开始和结束日期计算一段时间内的项目计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!