通过lapply和regex批量创建列到数据中的列,R表

au9on6nz  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(91)

我想在一些字符串后得到值,演示如下

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"))

我收到如下错误消息:

  • www.example.com中的错误match.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")*

但是我想不出怎么解决这个问题。谁能给予我点建议?

zrfyljdw

zrfyljdw1#

循环模式并使用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

相关问题