英文:
use a function in j position of data.table in R
问题
我一直在尝试使用一个函数,其参数是相同数据表的另一列,但遇到了问题。
pattern.1 <- regex("(?<=a)(.*)(?=c)")
DT <- data.table(x = c("abc", "adc", "abd"))
DT[, y := str_match(x, pattern.1)[1, 2]]
DT
例如,上面的代码产生了以下结果:
x y
1: abc b
2: adc b
3: abd b
但预期结果是:
x y
1: abc b
2: adc d
3: abd NA
有人知道如何解决这个问题吗,谢谢!
英文:
I've been having trouble using a function with it's argument being another column of of the same data.table.
pattern.1 <- regex("(?<=a)(.*)(?=c)")
DT <- data.table(x = c("abc", "adc", "abd"))
DT[, y := str_match(x, pattern.1)[1, 2]]
DT
for example, the above code give yield:
x y
1: abc b
2: adc b
3: abd b
but the expected result is:
x y
1: abc b
2: adc d
3: abd NA
Does anyone know how to fix this problem, tks!
答案1
得分: 1
你可以按照以下方式解决你的问题:
DT[, y := str_extract(x, pattern.1)]
# 或者(使用 str_match)
DT[, y := str_match(x, pattern.1)[, 1]]
备注
str_match
返回一个矩阵而不是向量;但是在你的问题中,你想要获得一个向量。- 在你的结果中,你在列
y
的所有行中获得了b
,因为你选择了str_match
返回的矩阵的第二列的第一个元素,然后进行了循环使用。你应该选择 所有 行和第一列(str_match(x, pattern.1)[, 1]
)。 - 函数
str_extract
更适用于你的问题,因为它返回一个向量(见下文)。
英文:
You could solve your problem as follow:
DT[, y := str_extract(x, pattern.1)]
# or (using str_match)
DT[, y := str_match(x, pattern.1)[, 1]]
x y
<char> <char>
1: abc b
2: adc d
3: abd <NA>
Remarks
str_match
returns a matrix and not a vector; but in your problem, you would like to get a vector.- You obtain
b
for all rows in columny
in your result because you selected the first element of the second column of the matrix returned bystr_match
, which was then recycled. You should select all rows and the first column (str_match(x, pattern.1)[, 1]
). - The function
str_extract
is more adapted to your problem because it returns a vector (see below)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论