创建Web会话

时间:2016-08-23 14:19:12

标签: r

我知道Randy在Sessonizing Log Data上有一篇很棒的帖子,但我正在努力调整基于30分钟不活动窗口生成会话ID的想法。

以下是我希望在R中生成的内容,最好是dplyr。我想要计算下面显示的session_id变量。

   dim_user_id       activity_date session_id
1      2665871 2014-12-31 19:00:08         1
2      2665871 2014-12-31 19:00:45         1
3      2665871 2014-12-31 19:01:01         1
4      2665877 2014-12-31 19:00:08         2
5      2665877 2014-12-31 19:00:33         2
6      2666612 2014-12-31 19:08:19         3
7      2666612 2014-12-31 19:08:32         3
8      2666612 2014-12-31 19:09:04         3
9      2666626 2014-12-31 19:00:25         4
10     2666627 2014-12-31 19:04:39         5

我尝试使用的代码是:

user_activity$sid = 1:nrow(user_activity)
user_activity$session_id = NA
# startTime = Sys.time()
user_activity = user_activity %>% 
  group_by(dim_user_id) %>% 
  arrange(activity_date) %>% 
  transform(lag_seconds = ifelse(lag(dim_user_id) == dim_user_id, 
                                 as.numeric(activity_date - lag(activity_date)), 
                                 9999)) %>% 
  mutate(session_id = ifelse(is.na(lag_seconds) | lag_seconds >= 1801, sid, lag(session_id)))

但我遇到的问题是我不相信这个值是按行设置的。我确实在rowwwise中探索了dplyr函数,但我被卡住了。

提前致谢。

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您正在寻找可以使用的group_indices,如下所示:

df %>% mutate(session_id = group_indices_(df, .dots="dim_user_id"))

编辑: 由于您的示例数据未提供一个用户具有多个30+时间差异的会话的情况,因此我使用了此更改的数据集:

df <- read.table(header=TRUE, text="dim_user_id date  time
2665871 2014-12-31 19:00:08
2665871 2014-12-31 19:00:45
2665871 2014-12-31 19:01:01
2665877 2014-12-31 19:00:08
2665877 2014-12-31 19:00:33
2666612 2014-12-31 19:08:19
2666612 2014-12-31 19:38:32
2666612 2014-12-31 19:39:04
2666626 2014-12-31 19:00:25
2666627 2014-12-31 19:04:39")

df$activity_date <- as.POSIXct(paste(df$date, df$time))
df$date <- NULL
df$time <- NULL

因此用户#2666612的延迟时间为30+。以下代码逐步计算您的session_id。我相信它可以缩短,但这只是为了澄清。

require(dplyr)
cuttoff <- 30*60 # 30 min times 60 seconds.
df %>% 
  # group by user_id
  group_by(dim_user_id) %>% 
  # Difference in seconds within a given user
  mutate(time_diff = c(0, diff(activity_date))) %>%
  # If the difference is >cutoff start new session
  mutate(session_num = cumsum(time_diff>cuttoff)) %>% 
  # ungroup to set group_indices data-wide instead of groupwide
  ungroup() %>% 
  # calculate group_indices based in user_id and session_num
  mutate(session_id = group_indices_(., .dots=c("dim_user_id", "session_num")))

结果是:

Source: local data frame [10 x 5]

   dim_user_id       activity_date time_diff session_num session_id
         (int)              (time)     (dbl)       (int)      (int)
1      2665871 2014-12-31 19:00:08         0           0          1
2      2665871 2014-12-31 19:00:45        37           0          1
3      2665871 2014-12-31 19:01:01        16           0          1
4      2665877 2014-12-31 19:00:08         0           0          2
5      2665877 2014-12-31 19:00:33        25           0          2
6      2666612 2014-12-31 19:08:19         0           0          3
7      2666612 2014-12-31 19:38:32      1813           1          4
8      2666612 2014-12-31 19:39:04        32           1          4
9      2666626 2014-12-31 19:00:25         0           0          5
10     2666627 2014-12-31 19:04:39         0           0          6