Message-ID: <CAAJSdji5gWgODaRM4K6E2tAWq83wz_gES7qYMeR=xpNS3S76Sw@mail.gmail.com>
Date: 2015-05-26T12:25:24Z
From: John McKown
Subject: How to pass a variable to a function which use variable name as a parameter
In-Reply-To: <CACnBwQu7aqGceHMVR1vZd2g-NXv_4ETNFSfoCTYEw+Rq0K6OnQ@mail.gmail.com>
On Tue, May 26, 2015 at 5:14 AM, wong jane <jane.wong083 at gmail.com> wrote:
> There are functions which use variable names as parameters in some R
> packages. However, if the variable name is stored in another variable, how
> can I pass this variable to the function. Taking the "rms" package as an
> example:
>
> ??
> 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)
> options(datadist='ddist')
> fit <- lrm(y ~ age)
> Predict(fit, age, np=4)
> options(datadist=NULL)
>
> Here "age" was a variable name passed to Predict() function, but if "age"
> was stored in variable "var", that is, var <- "age", how can I pass "var"
> to Predict() function? The purpose is that I want to change the parameter
> of Predict() in a loop.
>
>
?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)
--
My sister opened a computer store in Hawaii. She sells C shells down by the
seashore.
If someone tell you that nothing is impossible:
Ask him to dribble a football.
He's about as useful as a wax frying pan.
10 to the 12th power microphones = 1 Megaphone
Maranatha! <><
John McKown
[[alternative HTML version deleted]]