【问题标题】:not getting the same result from within the "j" parameter of a data.table从 data.table 的“j”参数中没有得到相同的结果
【发布时间】:2020-03-21 12:14:43
【问题描述】:

我正在从 data.table 的“j”参数中调用一个函数,但我得到的结果与我直接调用它的结果不同。这似乎与my function called in data.table j not returning expected results有关

get.lower.bound <- function (x) {
  rex <-
    regexpr (pattern = "((?<lower>[\\-+\\d*\\.,]*)%\\s*<\\s*)?X(\\s<\\s(?<uppper>[\\-+\\d\\.,]*)%)?",
             text = x,
             perl = TRUE)
  lower_bound <-
    substring(
      text = x,
      first = attr(rex, "capture.start")[2],
      last = attr(rex, "capture.start")[2] + attr(rex, "capture.length")[2] -1
    )
  lower_bound
}

dat <- data.table(
  A = c('1% < X < 2%', '4% < X',      'X < 8%' ),
  B = c('2% < X < 3%', '5% < X < 6%', '8% < X < 9%' ),
  C = c('3% < X < 4%', '6% < X < 7%', 'X < 10%' )
)

get.lower.bound(dat[1,'A'])                   # this returns 1; as I expect
get.lower.bound(dat[2,'A'])                   # this returns 4; as I expect
get.lower.bound(dat[3,'A'])                   # this returns a blank string; as I expect
dat[i = 1, j = .(lb1 = get.lower.bound(A))]   # this returns a data table of just one cell with 1 in it; as I expect
dat[i = 1:3, j = A]                           # this returns a character vector with the original strings in it; as I expect
dat[i = 1:3, j = .(lb1 = c(A))]               # this returns a data table with the original strings in it; as I expect
dat[i = 1:3, j = .(lb1 = get.lower.bound(A))] # this returns "1% <", "4% <", "X < 8"; but I expect a "1", "4" & ""

为什么最后一行没有给出我所期望的?我需要做什么才能得到我想要的东西?

【问题讨论】:

    标签: r data.table


    【解决方案1】:

    我认为这是因为您的函数 get.lower.bound 没有矢量化。如果按每一行对它进行分组然后传递函数,它会给出预期的输出。

    library(data.table)
    dat[, j = .(lb1 = get.lower.bound(A)),  by = 1:nrow(dat)]
    
    #   nrow lb1
    #1:    1   1
    #2:    2   4
    #3:    3    
    

    【讨论】:

      猜你喜欢
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-07
      • 2020-02-26
      相关资源
      最近更新 更多