[英]Count observations at any given date
I'm working with a dataset similar to the following:我正在使用类似于以下内容的数据集:
df <- data.frame(type = c("A", "A", "A", "A", "A", "B", "B", "B", "C", "D", "D", "D"),
start_date = as.Date(c("2010-02-01", "2011-03-15", "2011-09-15", "2015-01-01", "2015-05-15", "2009-01-01", "2015-07-14", "2016-06-30", "2012-01-15", "2010-04-05", "2010-08-01", "2012-04-01"), format = "%Y-%m-%d"),
end_date = as.Date(c("2010-12-31", "2011-07-31", "2014-04-04", "2015-02-15", "2016-12-15", "2013-02-16", "2015-12-31", "2016-12-31", "2015-09-17", "2010-04-10", "2010-09-30", "2013-12-31"), format = "%Y-%m-%d"))
I would like to count the number of observations at any given date.我想计算任何给定日期的观察次数。
Expected output预期输出
Essentially, I want my results to show that there is only one type
until 2010-02-01, then two until 2010-04-05, then three until 2010-04-10 etc, ie one column with the date (one row per day) and one column with the count of type
.从本质上讲,我希望我的结果表明只有一种
type
到 2010-02-01,然后两种到 2010-04-05,然后三种到 2010-04-10 等等,即一列带有日期(每行day) 和一列type
.
date count_of_type
2009-01-01 1
2009-01-02 1
2009-01-03 1
...
2010-01-31 1
2010-02-01 2
2010-02-02 2
...
2010-04-04 2
2010-04-05 3
2010-04-06 3
2010-04-07 3
2010-04-08 3
2010-04-09 3
2010-04-10 2
2010-04-11 2
...
I thought this would be would be easy to do, but can't figure it out... any idea?我认为这很容易做到,但无法弄清楚......知道吗?
Cheers,干杯,
An option would be to transmute
by taking the corresponding seq
uence of 'start_date', 'end_date' by day
and then get the count
一种选择是
transmute
通过采取相应的seq
由“起始日期”,“END_DATE”的uence day
,然后拿到count
library(tidyverse)
df %>%
transmute(date = map2(start_date, end_date, seq, by = '1 day')) %>%
unnest %>%
count(date)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.