![](/img/trans.png)
[英]r data.table lapply or for loop to create variables or generate columns
[英]batch create columns by lapply and regex to a column in data.table of R
我想在一些字符串之后获取值,演示如下
dt <- data.table(col.1 = c("a1, b2, c3, d4"))
x <- c("a", "b", "c")
dt[, (x) := lapply(FUN = str_match(string = .SD,
pattern = paste0("(?<=", x, ")([\\d])"))[, 2],
X = x),
.SDcols = "col.1"]
理想的结果看起来像这样
desirable <- data.table(col.1 = c("a1, b2, c3, d4"),
a = c("1"),
b = c("2"),
c = c("3"))
我收到如下错误信息:
* match.fun(FUN) 错误:
c("'str_match(string = .SD, pattern = paste0(\"(?<=\", x, \")([\\\\d])\"))[, ' is not a function, character or symbol", "' 2]' is not a function, character or symbol")*
但我不知道如何解决这个问题。 谁能给我一些hins?
遍历模式并使用str_match
提取值
library(data.table)
library(stringr)
dt[, (x) := lapply(paste0("(?<=", x, ")(\\d+)"),
\(x) str_match(col.1, x)[, 2])]
col.1 a b c
1: a1, b2, c3, d4 1 2 3
或者用strcapture
pat <- paste0(sprintf("%s(\\d+)", x), collapse = ".*")
cbind(dt, dt[, strcapture(pat, col.1, setNames(rep(list(integer()), 3), x))])
col.1 a b c
1: a1, b2, c3, d4 1 2 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.