r - 将 data.table 转换组合在一行中
问题描述
我写了一个 data.table 数据转换代码:
table_patterns <- table[, pattern := stringr::str_extract(LOG_MSG, "\\S+\\s+\\S+\\s+\\S+")]
table_patterns <- table_patterns[, pattern := coalesce(stringr::str_extract(pattern, "^create new|regulate values"),pattern)]
table_patterns <- table_patterns[, system_type := case_when(class%in% c("class1", "class2") ~ "creation_class",class%in% c("class3","class4") ~ "uploading class")]
table_patterns <- table_patterns[, source := case_when(message%in% c("adapting new values within system|data wrangling and transformation in system") | class %in% c("class5", "class6") | pattern %in% c("user1 has uploaded new text") ~ "text_uploading",class %in% c("class7") ~ "alt_class",TRUE ~ "self_search"), by = .(class, pattern, system_type,source,message_type)][,
如您所见,我创建了 table_patterns,然后对其进行了三次转换。我怎么能在一个过程中做到这一点。不修改三遍?不需要数据示例,它是关于代码语法的,所以不需要数据示例。
解决方案
随着data.table
,:=
修改到位。因此,无需将输出 ( <-
) 分配给另一个对象。多个参数的链接可以完成为
library(data.table)
table[, pattern := stringr::str_extract(LOG_MSG, "\\S+\\s+\\S+\\s+\\S+")
][, pattern := coalesce(stringr::str_extract(pattern,
"^create new|regulate values"),pattern), by = .(class, pattern)
][, system_type := case_when(class%in% c("class1", "class2") ~
"creation_class",
class%in% c("class3","class4") ~ "uploading class"), by = .(class, pattern)
][, source := case_when(message %in%
c("adapting new values within system|data wrangling and transformation in system") |
class %in% c("class5", "class6") |
pattern %in% c("user1 has uploaded new text") ~
"text_uploading",
class %in% c("class7") ~ "alt_class",
TRUE ~ "self_search"),
by = .(class, pattern, system_type,source,message_type)][]
推荐阅读
- swiftui - SwfitUI 中的 .onAppear 修饰符是否有相反的含义?
- python - 在 Ubuntu 机器上运行 Python 脚本会禁用网络端口
- c++ - 多个#include,不同的功能,相同的名称:如何区分它们?
- javascript - 无法使用 LeanFt +Java 自动化脚本单击按钮 (tag=>EDL-FILE-UPLOAD)
- javascript - Angular HTTP 拦截器测试 - 更改 window.location.href 值
- ssis - 当数据流位于 For Each 循环容器中时,记录集目标中的变量被替换了吗?
- ruby-on-rails - 为什么我在登录时收到缺少列的错误?
- centos - 如何在绑定接口中设置桥接适配器?
- python - 如何从字典列表中获取值?
- php - 我的 PHP 代码没有在我的 Ubuntu 操作系统上运行