[R] pass names from dataframe as arguments
Joshua Wiley
jwiley.psych at gmail.com
Mon May 17 23:29:29 CEST 2010
R users,
I am trying to write some functions where names from a dataframe are
arguments. I know it is done in functions like lm(), but I haven't
been able to figure out how unless I attach the dataframe first or
wrap the names in expression() so they are not evaluated immediately.
How can I get around this? For instance, here is a simple function:
myfun <- function(x, spliton, conditional, outcome, prob, above, ...) {
cutoff <- quantile(x=x[,paste(spliton)], probs=prob, ...)
ifelse(isTRUE(above),
data.sub <- subset(x=x, subset=(eval(spliton) >= cutoff) &
eval(conditional), select=eval(outcome)),
data.sub <- subset(x=x, subset=(eval(spliton) <= cutoff) &
eval(conditional), select=eval(outcome)))
M <- mean(x=data.sub, ...)
value <- c("mean"=M)
return(value)
}
set.seed(10)
test <- data.frame(a=rep(c(0,1), each=4), b=rnorm(8), c=1:8)
myfun(x=test, spliton=expression(b), conditional=expression(a==1),
outcome=expression(c), prob=2/3, above=TRUE) #this works
#I would like something like this to work
myfun(x=test, spliton=b, conditional= a==1, outcome=c, prob=2/3, above=TRUE)
Thanks for any advice,
Josh
--
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/
More information about the R-help
mailing list