我有这个数据框:
unixtime newsess
1 1412122086 1
2 1412122130 0
3 1412122191 0
4 1412122300 0
5 1412122443 0
6 1412122090 1
7 1412122124 0
8 1412122192 0
我需要这个:
unixtime newsess group
1 1412122086 1 1
2 1412122130 0 1
3 1412122191 0 1
4 1412122300 0 1
5 1412122443 0 1
6 1412122090 1 2
7 1412122124 0 2
8 1412122192 0 2
数据包含事件的有序时间戳,其中每个会话的第一个事件都用1表示。任务是为每个事件分配唯一的会话标识符。
如何在R中执行此操作?我已经用谷歌搜索了很多,但似乎我已经碰壁了。
我的示例数据可以通过以下方式创建:
mydf <- structure(list(unixtime = c(1412122086L, 1412122130L, 1412122191L,
1412122300L, 1412122443L, 1412122090L, 1412122124L, 1412122192L),
newsess = c(1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L)), .Names = c("unixtime", "newsess"),
class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6","7", "8"))
mydf$group <- cumsum(mydf$newsess)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句