在R中使用data.table的j位置上的函数。

huangapple go评论52阅读模式
英文:

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 &lt;- regex(&quot;(?&lt;=a)(.*)(?=c)&quot;)
  
DT &lt;- data.table(x = c(&quot;abc&quot;, &quot;adc&quot;, &quot;abd&quot;))
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]]

备注

  1. str_match 返回一个矩阵而不是向量;但是在你的问题中,你想要获得一个向量。
  2. 在你的结果中,你在列 y 的所有行中获得了 b,因为你选择了 str_match 返回的矩阵的第二列的第一个元素,然后进行了循环使用。你应该选择 所有 行和第一列(str_match(x, pattern.1)[, 1])。
  3. 函数 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
   &lt;char&gt; &lt;char&gt;
1:    abc      b
2:    adc      d
3:    abd   &lt;NA&gt;

Remarks

  1. str_match returns a matrix and not a vector; but in your problem, you would like to get a vector.
  2. You obtain b for all rows in column y in your result because you selected the first element of the second column of the matrix returned by str_match, which was then recycled. You should select all rows and the first column (str_match(x, pattern.1)[, 1]).
  3. The function str_extract is more adapted to your problem because it returns a vector (see below)

huangapple
  • 本文由 发表于 2023年5月28日 19:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定