如何使用data.table原生语法重写含字符串提取、分组采样的数据框转换逻辑
data.table 实现文本分组转换的需求与实现
基础数据说明
- 持有数据为两列结构的
data.table,包含ID、value两个字段,测试数据集可通过如下代码构造:
ID <- c(1,2,2,2,3,4,4) value <- c("he following object is masked from ‘package:purrr’. R is free software and comes with ABSOLUTELY NO WARRANTY", "Attaching package: ‘magrittr’. Natural language support but running in an English locale", "Attaching package: ‘DT’. Natural language support but running in an English locale", "Attaching package: ‘anytime’. Natural language support but running in an English locale", "package ‘ggplot2’ was built under R version 3.6.2. Type 'contributors()' for more information", "Warning messages:type 'demo()' for some demos, 'help()' for on-line help", "Warning messages:'help.start()' for an HTML browser interface to help") df <- data.table::data.table(ID, value)
现有dplyr实现逻辑与预期输出
当前基于dplyr的转换规则为:
- 从
value字段提取前三个连续非空白词段作为初始pattern值 - 对初始
pattern做二次匹配:如果命中^Attaching package:或Warning messages:规则则替换为匹配到的规则值,否则保留原提取结果 - 标记ID属于
c(1,5)的行,id_type值为extra_type,其余为缺失值 - 按
ID、pattern分组,每组随机抽取1条value作为示例值example - 对存在缺失的
pattern值,用同组的example值填充
对应dplyr实现代码:
df_patterns <- df %>% dplyr::mutate( pattern = stringr::str_extract(value, "\\S+\\s+\\S+\\s+\\S+"), pattern = dplyr::coalesce( stringr::str_extract(pattern, "^Attaching package:|Warning messages:"), pattern ), id_type = dplyr::case_when(ID %in% c(1, 5) ~ "extra_type") ) %>% dplyr::group_by(ID, pattern) %>% dplyr::summarise(example = sample(value, 1), .groups = "drop") %>% dplyr::mutate(pattern = dplyr::coalesce(pattern, example))
转换后的预期输出结构:
ID pattern example id_type 1 he following object he following object is masked from ‘package:purrr’. R is free software and comes with ABSOLUTELY NO WARRANTY extra_type 2 Attaching package: Attaching package: ‘anytime’. Natural language support but running in an English locale NA 3 package ‘ggplot2’ was package ‘ggplot2’ was built under R version 3.6.2. Type 'contributors()' for more information NA 4 Warning messages: Warning messages:'help.start()' for an HTML browser interface to help NA
需求目标
不使用mutate、group_by、summarise等dplyr系列函数,完全依靠data.table原生语法实现上述全部转换逻辑。
现有问题
自行尝试编写的代码存在语法问题,无法达到预期效果,尝试代码如下:
df_patterns <- df[, c("pattern", "id_type") := list( pattern = dplyr::coalesce( stringr::str_extract( pattern = stringr::str_extract(value, "\\S+\\s+\\S+\\s+\\S+"), "^Attaching package:|Warning messages:" ), pattern = stringr::str_extract(value, "\\S+\\s+\\S+\\s+\\S+") ), dplyr::case_when(ID %in% c(1, 5) ~ "extra_type") ), by = ID, pattern]
尝试代码的核心语法问题:
- data.table多列赋值时,
list()内元素按顺序匹配左侧指定的列名,不需要在函数传参时重复标注列名 - data.table的
by参数传入多列时,需要用.()语法包裹,直接以逗号分隔写在by后不符合语法规范 - 转换过程无需依赖dplyr函数,data.table有原生对应实现:
coalesce对应fcoalesce,case_when可通过逻辑判断直接赋值实现
原生data.table实现代码
# 加载data.table包 library(data.table) # 构造测试数据 ID <- c(1,2,2,2,3,4,4) value <- c("he following object is masked from ‘package:purrr’. R is free software and comes with ABSOLUTELY NO WARRANTY", "Attaching package: ‘magrittr’. Natural language support but running in an English locale", "Attaching package: ‘DT’. Natural language support but running in an English locale", "Attaching package: ‘anytime’. Natural language support but running in an English locale", "package ‘ggplot2’ was built under R version 3.6.2. Type 'contributors()' for more information", "Warning messages:type 'demo()' for some demos, 'help()' for on-line help", "Warning messages:'help.start()' for an HTML browser interface to help") df <- data.table(ID, value) # 步骤1:生成初始pattern字段 df[, pattern := stringr::str_extract(value, "\\S+\\s+\\S+\\s+\\S+")] # 步骤2:二次匹配校正pattern值 df[, pattern := fcoalesce( stringr::str_extract(pattern, "^Attaching package:|Warning messages:"), pattern )] # 步骤3:标记id_type df[, id_type := fifelse(ID %in% c(1,5), "extra_type", NA_character_)] # 步骤4:按ID、pattern、id_type分组,随机抽取单条value作为example df_patterns <- df[, .(example = sample(value, 1)), by = .(ID, pattern, id_type)] # 步骤5:填充缺失的pattern值 df_patterns[is.na(pattern), pattern := example]
内容的提问来源于stack exchange,提问作者french_fries
相关产品推荐
相关产品推荐

