R 索引数据框中的分组列

R 索引数据框中的分组列,r,dplyr,R,Dplyr,我有一个数据框如下 time site val 2014-09-01 00:00:00 2001 1 2014-09-01 00:15:00 2001 0 2014-09-01 00:30:00 2001 2 2014-09-01 00:45:00 2001 0 2014-09-01 00:00:00 2002 1 2014-09-01 00:15:00 2002

我有一个数据框如下

                     time  site   val

  2014-09-01 00:00:00  2001     1
  2014-09-01 00:15:00  2001     0
  2014-09-01 00:30:00  2001     2
  2014-09-01 00:45:00  2001     0
  2014-09-01 00:00:00  2002     1
  2014-09-01 00:15:00  2002     0
  2014-09-01 00:30:00  2002     2
  2014-09-02 00:45:00  2001     0
  2014-09-02 00:00:00  2001     1
  2014-09-02 00:15:00  2001     0
  2014-09-02 00:30:00  2001     2
  2014-09-02 00:45:00  2001     0
  2014-09-02 00:00:00  2002     1
  2014-09-02 00:15:00  2002     0
  2014-09-02 00:30:00  2002     2
  2014-09-02 00:45:00  2001     0
我想能够分组它的时间和地点,然后添加一个新的变量,将包括组的发生索引

                 time  site   val   h 

  2014-09-01 00:00:00  2001     1   1
  2014-09-01 00:15:00  2001     0   2
  2014-09-01 00:30:00  2001     2   3
  2014-09-01 00:45:00  2001     0   4
  2014-09-01 00:00:00  2002     1   1
  2014-09-01 00:15:00  2002     0   2
  2014-09-01 00:30:00  2002     2   3
  2014-09-02 00:45:00  2002     0   4
  2014-09-02 00:00:00  2001     1   1
  2014-09-02 00:15:00  2001     0   2
  2014-09-02 00:30:00  2001     2   3
  2014-09-02 00:45:00  2001     0   4
  2014-09-02 00:00:00  2002     1   1
  2014-09-02 00:15:00  2002     0   2
  2014-09-02 00:30:00  2002     2   3
  2014-09-02 00:45:00  2001     0   4

df <- structure(list(time = structure(c(1409522400, 1409523300, 1409524200, 
1409525100, 1409522400, 1409523300, 1409524200, 1409611500, 1409608800, 
1409609700, 1409610600, 1409611500, 1409608800, 1409609700, 1409610600, 
1409611500), class = c("POSIXct", "POSIXt"), tzone = ""), site = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("2001", 
"2002"), class = "factor"), val = c(1L, 0L, 2L, 0L, 1L, 0L, 2L, 
0L, 1L, 0L, 2L, 0L, 1L, 0L, 2L, 0L)), .Names = c("time", "site", 
"val"), row.names = c(NA, -16L), class = "data.frame")
时间站点val h
2014-09-01 00:00:00  2001     1   1
2014-09-01 00:15:00  2001     0   2
2014-09-01 00:30:00  2001     2   3
2014-09-01 00:45:00  2001     0   4
2014-09-01 00:00:00  2002     1   1
2014-09-01 00:15:00  2002     0   2
2014-09-01 00:30:00  2002     2   3
2014-09-02 00:45:00  2002     0   4
2014-09-02 00:00:00  2001     1   1
2014-09-02 00:15:00  2001     0   2
2014-09-02 00:30:00  2001     2   3
2014-09-02 00:45:00  2001     0   4
2014-09-02 00:00:00  2002     1   1
2014-09-02 00:15:00  2002     0   2
2014-09-02 00:30:00  2002     2   3
2014-09-02 00:45:00  2001     0   4

df使用
dplyr
。首先,我们创建一个列
id
从日期中提取日期(列
time
)。然后,我们按
站点
id
分组,并添加一个新变量
计数器
,计算这两组的出现次数

df$id <- as.factor(format(df$time,'%d'))
library(dplyr)
df %>% group_by(site, id) %>% mutate(counter = row_number()) 

我们可以使用
ave

df$h <- with(df, ave(val, cumsum(c(TRUE,diff(time)< 0)), FUN= seq_along))
df
#                  time site val h
#1  2014-09-01 03:30:00 2001   1 1
#2  2014-09-01 03:45:00 2001   0 2
#3  2014-09-01 04:00:00 2001   2 3
#4  2014-09-01 04:15:00 2001   0 4
#5  2014-09-01 03:30:00 2002   1 1
#6  2014-09-01 03:45:00 2002   0 2
#7  2014-09-01 04:00:00 2002   2 3
#8  2014-09-02 04:15:00 2001   0 4
#9  2014-09-02 03:30:00 2001   1 1
#10 2014-09-02 03:45:00 2001   0 2
#11 2014-09-02 04:00:00 2001   2 3
#12 2014-09-02 04:15:00 2001   0 4
#13 2014-09-02 03:30:00 2002   1 1
#14 2014-09-02 03:45:00 2002   0 2
#15 2014-09-02 04:00:00 2002   2 3
#16 2014-09-02 04:15:00 2001   0 4

df$h谢谢大家,我忘了解释我的请求,即需要为分组列time&site重置h列。我在我的原始问题中编辑了数据帧。你能
dput(data)
?我不明白你的第8行结果,也就是说,我不知道算法。请更正您的问题(不仅仅是每条评论),您是否可以检查预期输出中是否有任何打字错误,也不确定“site”中的最后一个元素是否为“2001”?
df$h <- with(df, ave(val, cumsum(c(TRUE,diff(time)< 0)), FUN= seq_along))
df
#                  time site val h
#1  2014-09-01 03:30:00 2001   1 1
#2  2014-09-01 03:45:00 2001   0 2
#3  2014-09-01 04:00:00 2001   2 3
#4  2014-09-01 04:15:00 2001   0 4
#5  2014-09-01 03:30:00 2002   1 1
#6  2014-09-01 03:45:00 2002   0 2
#7  2014-09-01 04:00:00 2002   2 3
#8  2014-09-02 04:15:00 2001   0 4
#9  2014-09-02 03:30:00 2001   1 1
#10 2014-09-02 03:45:00 2001   0 2
#11 2014-09-02 04:00:00 2001   2 3
#12 2014-09-02 04:15:00 2001   0 4
#13 2014-09-02 03:30:00 2002   1 1
#14 2014-09-02 03:45:00 2002   0 2
#15 2014-09-02 04:00:00 2002   2 3
#16 2014-09-02 04:15:00 2001   0 4