Skip to content
Prev 351204 / 398502 Next

How to pass a variable to a function which use variable name as a parameter

On Tue, May 26, 2015 at 5:14 AM, wong jane <jane.wong083 at gmail.com> wrote:

            
?Please turn off HMTL email. The forum software doesn't really like it.

What you want is the "get()" function.


var<-"age"

?
?
library(rms)
n <- 1000
age <- rnorm(n, 50, 10)
sex <- factor(sample(c('female','male'), n,TRUE))

y <- rnorm(n, 200, 25)
?#?
ddist <- datadist(age, sex)
?ddist <- datadist(get(var),sex)?
options(datadist='ddist')
fit <- lrm(y ~ age)
?#?
Predict(fit, age, np=4)
?Predict(fit,get(var),np=4)?
options(datadist=NULL)