Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据固定的日期序列将分组列添加到data.frame_R_Date_Dataframe_Rows - Fatal编程技术网

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

根据固定的日期序列将分组列添加到data.frame,r,date,dataframe,rows,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',

下面是我的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)
我需要创建一个第三列,将观察结果按两个连续日期分组

这里是我想要的输出:

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

有什么建议吗?

只要日期上没有空白,这就行了

#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

不,日期必须是连续的,并按2分组。问题是我在某些日期有多个ID。。。。