【发布时间】:2021-06-07 22:47:36
【问题描述】:
将一系列函数存储在 data.table 中然后有效地使用每个函数产生一些结果的正确方法是什么?
# create data
dt <- data.table(name = c('a', 'b', 'c'),
value1 = c(25, 35, 45),
value2 = c(50, 60, 70),
units1 = c(100, 200, 300),
units2 = c(300, 400, 500))
# function to create row-specific functions:
get_spline_fun <- function(data){
splinefunH(
x = c(0, data[['value1']]/data[['value2']], 5),
y = c(0, data['units1']/data['units2'], 1),
m = c(1.2, 1, 0))
}
# create functions and store in a new column
dt$spline_fun <- apply(
dt[, c('value1', 'value2', 'units1', 'units2')], 1, get_spline_fun)
# create x values
x <- seq(0, 5, 0.01)
# apply functions to x values
dt[, y := spline_fun(x)]
# Results in: Error in spline_fun(x) : could not find function "spline_fun"
【问题讨论】:
标签: r function data.table