R,如何保持行后,和/或值之间按组分割?

  • 本文关键字:之间 分割 何保持 r
  • 更新时间 :
  • 英文 :


对不起,由于某种原因,我的输出函数给出了无意义的输出。我有一个df与用户id和事件。我试图建立从事件$标识开始的每个用户ID的路径。我如何只保留前5行$标识后每个ID?输出应该包含每个ID加上接下来的5个事件。数据框已经按照userID和Date进行了排序,所以不需要这样做,只需要保持顺序。

的示例输出
ID1232下拉菜单

这是一个直接的分组操作,但有一个转折:至少一个user_id有多个$identify行,在这种情况下,您需要检查两个。

基地R

testset$group <- ave(testset$event, testset$user_id,
FUN = function(z) cumsum(z == "$identify"))
keep <- ave(testset$event, testset[,c("user_id", "group")],
FUN = function(z) {
if (length(z) && z[1] == "$identify") (seq_along(z) <= 5) else FALSE
}) == "TRUE"
testset[keep,]
#                                  user_id        datetime                          event group
# 25  0106d45f-deda-4205-894a-006c8afa5abc 9/21/21 9:43 AM                      $identify     1
# 26  0106d45f-deda-4205-894a-006c8afa5abc 9/21/21 9:43 AM                      Page View     1
# 27  0106d45f-deda-4205-894a-006c8afa5abc 9/21/21 9:43 AM              Onboarding - Next     1
# 28  0106d45f-deda-4205-894a-006c8afa5abc 9/21/21 9:44 AM Dimension Choice - Choice Made     1
# 29  0106d45f-deda-4205-894a-006c8afa5abc 9/21/21 9:44 AM                      Page View     1
# 40  01370990-cc35-4be4-8927-39e83b7f1e76 9/21/21 8:32 PM                      $identify     1
# 41  01370990-cc35-4be4-8927-39e83b7f1e76 9/21/21 8:32 PM                      Page View     1
# 42  01370990-cc35-4be4-8927-39e83b7f1e76 9/21/21 8:32 PM              Onboarding - Next     1
# 43  01370990-cc35-4be4-8927-39e83b7f1e76 9/21/21 8:32 PM                      Page View     1
# 44  01370990-cc35-4be4-8927-39e83b7f1e76 9/21/21 8:32 PM               2D - Agenda Icon     1
# 108 01370990-cc35-4be4-8927-39e83b7f1e76 9/22/21 8:52 AM                      $identify     2
# 109 01370990-cc35-4be4-8927-39e83b7f1e76 9/22/21 8:52 AM                      Page View     2
# 110 01370990-cc35-4be4-8927-39e83b7f1e76 9/22/21 8:52 AM               3D - Agenda Icon     2
# 111 01370990-cc35-4be4-8927-39e83b7f1e76 9/22/21 8:52 AM                      Page View     2
# 112 01370990-cc35-4be4-8927-39e83b7f1e76 9/22/21 8:52 AM      Agenda - Join the Session     2

dplyr

与另一个答案类似,以不同的方式处理多个$identify实例

library(dplyr)
testset %>%
group_by(user_id) %>%
mutate(group = cumsum(event == "$identify")) %>%
group_by(user_id, group) %>%
filter(event[1] == "$identify" & row_number() <= 5) %>%
ungroup()

data.table

library(data.table)
testsetDT <- as.data.table(testset)
testsetDT[, group := cumsum(event == "$identify"), by = user_id
][, .SD[event[1] == "$identify" & seq_len(.N) <= 5,], by = .(user_id, group)
]

对于第二个需求(返回两个event之间的行),很难完全测试,因为您的数据不包括"Welcome...",但我将使用"Page View"代替:

testset %>%
group_by(user_id) %>%
mutate(group = cumsum(event == "Page View")) %>%
group_by(user_id, group) %>%
filter(event[1] == "Page View" &
"$identify" %in% event &
(event == "$identify" | !cumany(event == "$identify"))) %>%
ungroup()
# # A tibble: 5 x 4
#   user_id                              datetime         event                      group
#   <chr>                                <chr>            <chr>                      <int>
# 1 0106d45f-deda-4205-894a-006c8afa5abc 9/21/21 11:55 AM Page View                      5
# 2 0106d45f-deda-4205-894a-006c8afa5abc 9/21/21 9:43 AM  $identify                      5
# 3 01370990-cc35-4be4-8927-39e83b7f1e76 9/22/21 8:51 AM  Page View                     25
# 4 01370990-cc35-4be4-8927-39e83b7f1e76 9/22/21 8:51 AM  Presentation - Close Modal    25
# 5 01370990-cc35-4be4-8927-39e83b7f1e76 9/22/21 8:52 AM  $identify                     25

如果需要,可以将类似的机制用于base R和data.table解决方案。


数据
testset <- structure(list(user_id = c("0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "0106d45f-deda-4205-894a-006c8afa5abc", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76", "01370990-cc35-4be4-8927-39e83b7f1e76"), datetime = c("9/21/21 10:10 AM", "9/21/21 10:10 AM", "9/21/21 10:10 AM", "9/21/21 10:10 AM", "9/21/21 10:10 AM", "9/21/21 10:10 AM", "9/21/21 10:10 AM", "9/21/21 10:10 AM", "9/21/21 10:11 AM", "9/21/21 10:12 AM", "9/21/21 10:12 AM", "9/21/21 10:13 AM", "9/21/21 10:13 AM", "9/21/21 10:13 AM", "9/21/21 10:13 AM", "9/21/21 10:13 AM", "9/21/21 10:14 AM", "9/21/21 10:14 AM", "9/21/21 10:14 AM", "9/21/21 10:14 AM", "9/21/21 10:27 AM", "9/21/21 11:47 AM", "9/21/21 11:55 AM", "9/21/21 11:55 AM", "9/21/21 9:43 AM", "9/21/21 9:43 AM", "9/21/21 9:43 AM", "9/21/21 9:44 AM", "9/21/21 9:44 AM", "9/21/21 9:46 AM", "9/21/21 9:46 AM", "9/21/21 9:46 AM", "9/21/21 9:46 AM", "9/21/21 9:47 AM", "9/21/21 9:47 AM", "9/21/21 9:47 AM", "9/21/21 9:50 AM", "9/21/21 9:50 AM", "9/21/21 9:50 AM", "9/21/21 8:32 PM", "9/21/21 8:32 PM", "9/21/21 8:32 PM", "9/21/21 8:32 PM", "9/21/21 8:32 PM", "9/21/21 8:32 PM", "9/21/21 8:36 PM", "9/21/21 8:36 PM", "9/21/21 8:36 PM", "9/21/21 8:36 PM", "9/21/21 8:37 PM", "9/21/21 8:37 PM", "9/21/21 8:37 PM", "9/21/21 8:38 PM", "9/22/21 10:00 AM", "9/22/21 10:00 AM", "9/22/21 10:01 AM", "9/22/21 10:01 AM", "9/22/21 10:01 AM", "9/22/21 10:01 AM", "9/22/21 10:02 AM", "9/22/21 10:27 AM", "9/22/21 10:27 AM", "9/22/21 10:27 AM", "9/22/21 10:27 AM", "9/22/21 10:27 AM", "9/22/21 10:30 AM", "9/22/21 10:30 AM", "9/22/21 10:30 AM", "9/22/21 10:30 AM", "9/22/21 10:31 AM", "9/22/21 10:58 AM", "9/22/21 10:58 AM", "9/22/21 10:58 AM", "9/22/21 10:58 AM", "9/22/21 10:58 AM", "9/22/21 10:58 AM", "9/22/21 10:58 AM", "9/22/21 2:30 PM", "9/22/21 2:30 PM", "9/22/21 2:30 PM", "9/22/21 2:30 PM", "9/22/21 2:30 PM", "9/22/21 2:30 PM", "9/22/21 2:30 PM", "9/22/21 2:30 PM", "9/22/21 7:48 AM", "9/22/21 8:00 AM", "9/22/21 8:00 AM", "9/22/21 8:01 AM", "9/22/21 8:01 AM", "9/22/21 8:39 AM", "9/22/21 8:39 AM", "9/22/21 8:39 AM", "9/22/21 8:49 AM", "9/22/21 8:49 AM", "9/22/21 8:49 AM", "9/22/21 8:49 AM", "9/22/21 8:50 AM", "9/22/21 8:50 AM", "9/22/21 8:51 AM", "9/22/21 8:51 AM", "9/22/21 8:51 AM", "9/22/21 8:51 AM", "9/22/21 8:51 AM", "9/22/21 8:51 AM", "9/22/21 8:51 AM", "9/22/21 8:51 AM", "9/22/21 8:52 AM", "9/22/21 8:52 AM", "9/22/21 8:52 AM", "9/22/21 8:52 AM", "9/22/21 8:52 AM", "9/22/21 8:52 AM", "9/22/21 8:52 AM", "9/22/21 8:52 AM", "9/22/21 8:53 AM", "9/22/21 8:56 AM", "9/22/21 8:56 AM", "9/22/21 8:56 AM", "9/22/21 8:57 AM", "9/22/21 8:57 AM", "9/22/21 8:57 AM", "9/22/21 8:57 AM", "9/22/21 8:57 AM", "9/22/21 8:57 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:58 AM", "9/22/21 8:59 AM", "9/22/21 9:00 AM", "9/22/21 9:00 AM", "9/22/21 9:02 AM", "9/22/21 9:02 AM", "9/22/21 9:02 AM", "9/22/21 9:11 AM", "9/22/21 9:12 AM", "9/22/21 9:12 AM", "9/22/21 9:16 AM", "9/22/21 9:16 AM"), event = c("Page View", "Dropdown Menu - Open", "Dropdown Menu - Settings", "Dropdown Menu - View Profile", "Profile - Edit", "Profile - Close", "Dropdown Menu - Directory", "Page View", "Directory - Close", "2D - Directory Icon", "Page View", "Directory - Close", "Dropdown Menu - Open", "Dropdown Menu - Settings", "Dropdown Menu - View Profile", "Profile - Edit", "Profile' - Save", "Dropdown Menu - Help", "Page View", "Help - Close", "Chat - Search User", "Dropdown Menu - Open", "Dropdown Menu - Agenda", "Page View", "$identify", "Page View", "Onboarding - Next", "Dimension Choice - Choice Made", "Page View", "Dimension Choice - Choice Made", "Dropdown Menu - Open", "Dropdown Menu - Settings", "Dropdown Menu - Sound Off", "Chat - Search User", "Chat - Invite a Person to Groupchat", "Chat - Enter Message", "Page View", "Dimension Choice - Choice Made", "Chat - Join a Videochat", "$identify", "Page View", "Onboarding - Next", "Page View", "2D - Agenda Icon", "Page View", "Agenda - Close", "2D - Directory Icon", "Page View", "Profile - View", "Profile - Close", "Directory - Next page participant", "Directory - Filter", "Directory - Close", "Presentation - Watch Presentation", "Page View", "Personal Agenda - Join The Session", "Page View", "Presentation - Open Modal", "Page View", "Presentation - Watch Presentation", "Presentation - Close Modal", "Page View", "Dropdown Menu - Open", "Dropdown Menu - My Agenda", "Page View", "Presentation - Open Modal", "Page View", "Personal Agenda - Join The Session", "Page View", "Presentation - Watch Presentation", "Presentation - Close Modal", "Page View", "Dropdown Menu - Open", "Dropdown Menu - My Agenda", "Page View", "Personal Agenda - Remove from my Agenda", "Page View", "Dropdown Menu - Open", "Dropdown Menu - My Agenda", "Page View", "Dropdown Menu - Open", "Dropdown Menu - Agenda", "Page View", "Agenda - Past Presentations", "Agenda - Close", "Page View", "Login - Login Button", "Page View", "2D - Agenda Icon", "Page View", "Agenda - Close", "Page View", "2D - Agenda Icon", "Presentation - Open Modal", "Agenda - Join the Session", "Page View", "Presentation - Watch Presentation", "Page View", "Presentation - Close Modal", "2D - Agenda Icon", "Page View", "Agenda - Join the Session", "Presentation - Open Modal", "Page View", "Presentation - Watch Presentation", "Page View", "Presentation - Close Modal", "$identify", "Page View", "3D - Agenda Icon", "Page View", "Agenda - Join the Session", "Page View", "Presentation - Open Modal", "Page View", "Presentation - Watch Presentation", "Presentation - Close Modal", "Page View", "Dropdown Menu - Open", "Dropdown Menu - My Agenda", "Page View", "Dropdown Menu - Open", "Dropdown Menu - Agenda", "Page View", "Agenda - Add to my Agenda", "Agenda - Close", "Dropdown Menu - Open", "Page View", "Dropdown Menu - My Agenda", "Page View", "Dropdown Menu - Open", "Dropdown Menu - Agenda", "Page View", "Agenda - Add to my Agenda", "Agenda - Close", "Rooms Nav - Open", "3D - Join a Session Icon", "Presentation - Open Modal", "Page View", "Presentation - Watch Presentation", "Page View", "Presentation - Watch Presentation", "Personal Agenda - Join The Session", "Page View", "Presentation - Watch Presentation", "Profile - View", "Profile - Close", "Presentation - Watch Presentation", "Page View", "Presentation - Close Modal")), row.names = c(NA, -150L), class = "data.frame")

相关内容

  • 没有找到相关文章

最新更新