使用字符输入在 R 中定义函数
Use character input to define function in R
我有一个输入数据集,它可能看起来像这样:
DF=data.frame(
Variable = c("Test1", "Test2", "Test3"),
Distribution = c("Normal", "Exponential","Poisson"),
Variable = c(2, 3, 4),
SD = c(2, NA, NA))
我想使用数据框 DF
中给出的分布来使用随机概率函数(例如 rnorm
rexp
和 rbinom
)。
那么,如何将文本输入转换为正确的函数?
如果合适,我想使用 Variable
和 SD
列中的相应值作为平均 values/standard 偏差。
类似于:
DF=data.frame(
Variable = c("Test1", "Test2", "Test3"),
Distribution = c("Normal", "Exponential","Poisson"),
VariablePrm = c(2, 3, 4),
SD = c(2, NA, NA),
stringsAsFactors = FALSE)
# functions-lookup
fun_vec <- c("rnorm", "rexp", "rpois")
names(fun_vec) <- c("Normal", "Exponential", "Poisson")
DF$fun <- fun_vec[DF$Distribution]
# create expr
my_expr <- function(x) {
txt <- paste0(x[1], "<-", x[5], "(", 10, ", ", x[3],
ifelse(is.na(x[4]), "",
paste0(", ", x[4])), ")")
}
want <- apply(DF, 1, function(x) eval(parse(text = my_expr(x))))
colnames(want) <- DF$Variable
want
@r.user.05apr 解决方案有效,但涉及一些此处不需要的表达式解析。通过创建一个函数列表以便稍后使用它们,我们可能会更容易做到这一点。
# generating data:
DF=data.frame(
Variable = c("Test1", "Test2", "Test3"),
Distribution = c("Normal", "Exponential","Poisson"),
VariablePrm = c(2, 3, 4),
SD = c(2, NA, NA),
stringsAsFactors = FALSE)
# creating function list and selecting this functions by Distribution column
fun_vec <- c(Normal=rnorm, Exponential=rexp, Poisson=rpois)
DF$fun <- fun_vec[DF$Distribution]
# if SD is NA then simply call function only with variablePrm
# else call with sd
# 10 is the number of observations to generate
generate <- function(x) {
if(is.na(x$SD)){
x$fun(10, x$VariablePrm)
}else{
x$fun(10, x$VariablePrm, x$SD)
}
}
# if we apply this functions to each row we will get matrix of results
# each column will have 10 rows of generated data for previously selected distribution
apply(DF, 1, generate)
我有一个输入数据集,它可能看起来像这样:
DF=data.frame(
Variable = c("Test1", "Test2", "Test3"),
Distribution = c("Normal", "Exponential","Poisson"),
Variable = c(2, 3, 4),
SD = c(2, NA, NA))
我想使用数据框 DF
中给出的分布来使用随机概率函数(例如 rnorm
rexp
和 rbinom
)。
那么,如何将文本输入转换为正确的函数?
如果合适,我想使用 Variable
和 SD
列中的相应值作为平均 values/standard 偏差。
类似于:
DF=data.frame(
Variable = c("Test1", "Test2", "Test3"),
Distribution = c("Normal", "Exponential","Poisson"),
VariablePrm = c(2, 3, 4),
SD = c(2, NA, NA),
stringsAsFactors = FALSE)
# functions-lookup
fun_vec <- c("rnorm", "rexp", "rpois")
names(fun_vec) <- c("Normal", "Exponential", "Poisson")
DF$fun <- fun_vec[DF$Distribution]
# create expr
my_expr <- function(x) {
txt <- paste0(x[1], "<-", x[5], "(", 10, ", ", x[3],
ifelse(is.na(x[4]), "",
paste0(", ", x[4])), ")")
}
want <- apply(DF, 1, function(x) eval(parse(text = my_expr(x))))
colnames(want) <- DF$Variable
want
@r.user.05apr 解决方案有效,但涉及一些此处不需要的表达式解析。通过创建一个函数列表以便稍后使用它们,我们可能会更容易做到这一点。
# generating data:
DF=data.frame(
Variable = c("Test1", "Test2", "Test3"),
Distribution = c("Normal", "Exponential","Poisson"),
VariablePrm = c(2, 3, 4),
SD = c(2, NA, NA),
stringsAsFactors = FALSE)
# creating function list and selecting this functions by Distribution column
fun_vec <- c(Normal=rnorm, Exponential=rexp, Poisson=rpois)
DF$fun <- fun_vec[DF$Distribution]
# if SD is NA then simply call function only with variablePrm
# else call with sd
# 10 is the number of observations to generate
generate <- function(x) {
if(is.na(x$SD)){
x$fun(10, x$VariablePrm)
}else{
x$fun(10, x$VariablePrm, x$SD)
}
}
# if we apply this functions to each row we will get matrix of results
# each column will have 10 rows of generated data for previously selected distribution
apply(DF, 1, generate)