我有一个来自游戏应用程序的数据集。 我想将我的应用程序数据集划分为多个会话。新的会话以appstart或appresume开始。它大多以appclose结尾,但并非总是如此。
记录结构如下:
appdata <- data.frame(userid = c(1,1,1,1,1,1,1), activity = c("appstart","appclose","appresume","run","appstart", "run", "run"),points = c(263,275,278,321, 322, 412, 498))
appdata
userid activity points
1 1 appstart 263
2 1 appclose 275
3 1 appresume 278
4 1 run 321
5 1 appstart 322
6 1 run 412
7 1 run 498
我希望它看起来像这样:
appdata_new <- data.frame(userid = c(1,1,1), session = c(1,2,3), mean_points = c(269, 299.5, 410.67))
appdata_new
userid session mean_points
1 1 1 269.00
2 1 2 299.50
3 1 3 410.67
根据用户的评论,我尝试了以下操作,但不适用于我的数据集:
appdata %>% group_by(userid, session = cumsum(lag(activity == "appstart", default = FALSE) | cumsum(lag(activity == "appresume", default = FALSE))) %>% summarise(mean_points = mean(points))