根据固定的日期序列将分组列添加到data.frame

时间:2017-05-01 20:41:31

标签: r date dataframe rows

这是我的data.frame的一个例子:

df = read.table(text = 'ID  Date
1  1980-10-01
2  1980-10-01
2  1980-10-02
3  1980-10-02
4  1980-10-03
5  1980-10-04
5  1980-10-05
5  1980-10-06
6  1980-10-06
7  1980-10-07
7  1980-10-08
8  1980-10-09
9  1980-10-10
10  1980-10-10', header = TRUE)

我需要创建第3列,按照连续2个日期的组对观察进行分组。

这是我想要的输出:

ID Date       Group
1  1980-10-01  1
2  1980-10-01  1 
2  1980-10-02  1
3  1980-10-02  1
4  1980-10-03  2
5  1980-10-04  2
5  1980-10-05  3
5  1980-10-06  3
6  1980-10-06  3
7  1980-10-07  4
7  1980-10-08  4
8  1980-10-09  5
9  1980-10-10  5
10  1980-10-10  5

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

只要日期没有空白,这就应该有效。

#Obtain the unique dates and assign grouping codes to them
#Also assign them name (the value of unique date)
vec = setNames(object = ceiling(seq_along(unique(as.Date(df$Date)))/2),
               nm = unique(as.Date(df$Date)))

#Use match to lookup grouping codes from the vector
df$group = vec[match(df$Date, names(vec))]
df
#   ID       Date group
#1   1 1980-10-01     1
#2   2 1980-10-01     1
#3   2 1980-10-02     1
#4   3 1980-10-02     1
#5   4 1980-10-03     2
#6   5 1980-10-04     2
#7   5 1980-10-05     3
#8   5 1980-10-06     3
#9   6 1980-10-06     3
#10  7 1980-10-07     4
#11  7 1980-10-08     4
#12  8 1980-10-09     5
#13  9 1980-10-10     5
#14 10 1980-10-10     5