将函数应用于每行data.table,最佳方法

时间:2017-11-11 21:39:49

标签: r data.table mapply

我有一些像这样定义的曲线

library(data.table)
basesheet <- data.table(
  ID_CURVE = c("a","a","a","b","b","b"), 
  TIME = rep(seq(1:3),2), 
  value = c(rep(0.1,3),rep(0.2,3)),
  extrapolate=rep(c(0,2.5,6),2))

我的问题是推断TIMExvalue定义的点在时间点“外推”的线性近似。

我正考虑将数据导入此结构

aaa <- basesheet[,
  .(TIMES = list(c(TIME)), VALUESTR = list(c(value)), EXTRA = list(c(extrapolate)))
, by =ID_CURVE]

给出了

ID_CURVE TIMES    VALUESTR       EXTRA
1:        a 1,2,3 0.1,0.1,0.1 0.0,2.5,6.0
2:        b 1,2,3 0.2,0.2,0.2 0.0,2.5,6.0

然后对每行应用近似函数以接收结果,如

aaa[, value := mapply(approx, TIMES, y=VALUESTR, EXTRA, method = linear",
                  rule = 2)]

但我无法弄清楚如何正确地做事,有人能提出解决方案或更好的方法吗?

0 个答案:

没有答案