[英]How to evaluate one argument of a function call using another argument of that call, and an object created inside the function environment in R?
我正在嘗試編寫一個將表達式作為參數的函數,然后在1)函數的另一個參數和2)在函數內部創建的對象的上下文中評估該表達式。
我在使環境正常工作時遇到了一些問題。 有誰知道如何做到這一點?
myfun <- function(es = .5, model = es * x[, 1]){
x <- matrix(rnorm(300), ncol = 3)
mu <- eval(model)
mu
}
myfun(es = .8, model = es * x[, 1] + es * x[, 1]^2 + es * x[, 1]^3)
錯誤結果:eval(model)中的錯誤:找不到對象'es'
有什么建議么?
嘗試substitute
:
myfun <- function(es = .5, model = es * x[, 1]){
x <- matrix(rnorm(300), ncol = 3)
mu <- eval(substitute(model))
mu
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.