基于时差的R分组 [英] R grouping based on time difference
问题描述
这是我的数据框:
df <- data.frame(col_1 = c('11/13/2007', '11/17/2007', '11/19/2007', '11/25/2007', '11/28/2007'),
col_2 = c('A', 'B', 'C', 'D', 'E'))
我想添加一列,该列将使用col_1
中日期的时差对元素进行分组.例如,第一行,第二行和第三行将位于组1中,因为日期相差仅不到5天(在每个连续日期之间),而第四行和第五行将在组2中.由于两个连续的日期,我们将得到两个组. 2007年11月19日"和"2007年11月25日"相差5天以上.
I would like to add column, which would group elements using time difference of dates in col_1
. For example first, second and third rows will be in group 1, since dates differ only by less than 5 days (between each consecutive dates) and row four and five will be in group 2. We will get two groups since two consecutive dates '11/19/2007' and '11/25/2007' differ by more than 5 days.
我可以计算日期之间的日差,但是现在确定如何创建分组.我更喜欢使用dplyr
的解决方案,但是任何建议都是值得的.
I can compute day difference between dates, but now sure how to create grouping. I would prefer solution with dplyr
, but any piece of advice is appreciated.
推荐答案
我认为您可以创建组,而不必做任何特别的事情.
I think you could create the groups without have to do anything particularly fancy.
首先我们清理col_1
,然后获取组.注意,我创建lag_time_diff
以帮助提高可读性,但是如果需要,您可以选择将其直接放在cumsum
中.
First we clean col_1
then get the groups. Note I create lag_time_diff
to help with readability but you can choose to put it directly in the cumsum
if you want.
df$col_1 <- as.POSIXct(df$col_1, format = "%m/%d/%Y")
lag_time_diff <- difftime(df$col_1, lag(df$col_1, default = df$col_1[1]), units = "days")
df$group <- cumsum(ifelse(lag_time_diff>5,1,0))
df
# col_1 col_2 group
#1 2007-11-13 A 0
#2 2007-11-17 B 0
#3 2007-11-19 C 0
#4 2007-11-25 D 1
#5 2007-11-28 E 1
所有这些操作是检查时间上的滞后差是否大于5,是否为1索引,否则保持相同的值.
All this does is check if the lagged difference in times is >5, if it is it indexes by 1 otherwise it keeps the same value.
这篇关于基于时差的R分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!