我想在一些字符串后得到值,演示如下
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")*
但是我想不出怎么解决这个问题。谁能给予我点建议?
1条答案
按热度按时间zrfyljdw1#
循环模式并使用
str_match
提取值或使用
strcapture