我从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'])
get.lower.bound(dat[2,'A'])
get.lower.bound(dat[3,'A'])
dat[i = 1, j = .(lb1 = get.lower.bound(A))]
dat[i = 1:3, j = A]
dat[i = 1:3, j = .(lb1 = c(A))]
dat[i = 1:3, j = .(lb1 = get.lower.bound(A))]
为什么最后一句话没有给我我想要的?我需要做什么才能得到我想要的?