我希望在一个时间序列中计算组内每个子组的计数/总和。
I'm hoping to calculate count/sum of each subgroup within groups over a time series.
我的问题与这个问题非常相似 随时间序列滚动事件数 。
My question is very similar to this question Rolling Count of Events Over Time Series.
抱歉,我一直在寻找方法来计算时间范围内(第1组的当前日期和前一个N(例如4) ) 天)。我想对组2中的每个子类型重复此过程,即组2是一个较大的组,可能/可能不包含组1中的所有类别。
Apologies for cross-posting, I have been looking for ways to count events for each category in group 1 within a time range (Present date and the previous N (say 4) days). I want to repeat this process for every subtype in group 2, i.e. Group 2 is a larger group that may/may not contains all the categories within Group 1.
,如果我们有一个如下所示的数据框
For example, if we have a data frame that looks like the following
dates = as.Date(c("2011-10-09", "2011-10-15", "2011-10-16", "2011-10-18", "2011-10-21", "2011-10-22", "2011-10-24")) group1=c("A", "A", "A", "A", "L", "L", "A") group2=c("I", "I", "I", "I", "I", "I", "II") df1 <- data.frame(dates, group1, group2)寻找与此类似的输出。 (编辑)最终,我想散布数据集,以便将组1中的类别放在单独的列中,并根据日期和组2排列行。如何确保组1类别的计数结转到新行(并满足上述时间范围)?
And I'm looking for output similar to this. (Edited) Eventually, I want to spread my dataset so that I will have categories in Group 1 in separate columns, and arrange rows according to dates and Group 2. How can I make sure the count of Group 1 categories is carried forward to the new row (and satisfy the timeframe stated above)?
dates group1 group2 count (A) count (L) 1 2011-10-09 A I 1 0 2 2011-10-15 A I 1 0 3 2011-10-16 A I 2 0 4 2011-10-18 A I 3 0 5 2011-10-21 L I 0 1 6 2011-10-22 L I 0 2 7 2011-10-24 A II 1 0谢谢!
推荐答案如果要显示组1和组2中所有事件的发生次数在活动开始前的4天里,您可以只 group_by 两个活动组,然后计数(使用 sapply 分别访问每个日期)。
If you are trying to show the count of all occurrences of events from group1 and group2 in the 4 days leading up to an event, you can just group_by the two event groups and then count (using sapply to access each date separately).
df1 %>% group_by(group1, group2) %>% mutate(count = sapply(dates , function(x){ sum(dates <= x & dates > (x-4)) }))返回值:
dates group1 group2 count <date> <fctr> <fctr> <int> 1 2011-10-09 A I 1 2 2011-10-15 A I 1 3 2011-10-16 A I 2 4 2011-10-18 A I 3 5 2011-10-21 L I 1 6 2011-10-22 L I 2 7 2011-10-24 A II 1更多推荐
按多个条件对时间序列中的事件类型进行计数
发布评论