You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R语言分析用户表单请求路径并标记未成功提交项

R代码方案:标记表单请求发起与提交状态

给定用户行为数据集,我们需要识别createrequestButtonClick(表单请求发起事件)与SendButtonClick(提交成功事件)的先后关系,同时标记发起请求后未完成提交的情况,支持同一会话内多次发起请求的场景。

示例数据集

# 加载示例数据
df <- structure(list(session_id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), 
                     User_ID = c("123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "345", "345", "345", "345", "345", "345", "345", "345", "345", "345", "345"), 
                     Page = c("home", "contact", "createrequestButtonClick", "requestform", "requestform", "FormValueChange", "FormContactSelection", "FormValueChange", "SendButtonClick", "home", "home", "contact", "createrequestButtonClick", "requestform", "FormValueChange", "SendButtonClick", "contact", "createrequestButtonClick", "requestform", "FormValueChange", "SendButtonClick"), 
                     Path_ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), 
                     Path_Length = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L)), 
                row.names = c(NA, -21L), 
                class = c("tbl_df", "tbl", "data.frame"))

解决方案代码

我们使用tidyverse工具链处理序列数据,核心思路是按会话分组后,为每个发起请求事件匹配后续最近的提交事件:

library(tidyverse)

# 1. 按会话分组,为每个请求发起事件分配唯一ID
df_processed <- df %>%
  group_by(session_id) %>%
  mutate(
    # 标记请求发起事件
    is_request_init = Page == "createrequestButtonClick",
    # 为每个请求生成唯一ID(同会话内递增)
    request_id = cumsum(is_request_init)
  ) %>%
  ungroup() %>%
  # 仅保留请求发起及后续相关事件(可选,减少计算量)
  filter(request_id > 0 | Page == "SendButtonClick") %>%
  group_by(session_id, request_id) %>%
  mutate(
    # 检查当前请求组内是否存在提交事件
    has_submit = any(Page == "SendButtonClick"),
    # 获取提交事件的Path_ID(若存在)
    submit_path_id = ifelse(has_submit, max(Path_ID[Page == "SendButtonClick"]), NA),
    # 标记是否为未提交的请求发起事件
    is_unsubmitted_request = is_request_init & !has_submit
  ) %>%
  ungroup()

# 查看处理结果
print(df_processed)

结果说明

处理后的数据集新增以下字段:

  • is_request_init:标记当前行是否为请求发起事件
  • request_id:同会话内每个请求的唯一标识(从1开始递增)
  • has_submit:该请求是否完成提交
  • submit_path_id:提交事件对应的Path_ID(未提交则为NA)
  • is_unsubmitted_request:标记发起后未提交的请求事件

如果需要单独提取未提交的请求记录,可以使用:

unsubmitted_requests <- df_processed %>%
  filter(is_unsubmitted_request) %>%
  select(session_id, User_ID, Path_ID, request_id)

print(unsubmitted_requests)

内容的提问来源于stack exchange,提问作者user2845095

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 06:33:27