You can do something like: aaa <- function(data, w=w) { if (class(w) %in% c("integer", "numeric", "double")) { out <- mean(w) } else { out <- mean(data[, w]) } return(out) }
(there are some typos in your function you may want to double check, too) On Tue, Jun 23, 2015 at 5:39 PM, Steven Yen <sye...@gmail.com> wrote: > mydata<-data.frame(matrix(1:20,ncol=2)) > colnames(mydata) <-c("v1","v2") > summary(mydata) > > aaa<-function(data,w=w){ > if(is.vector(w)){ > out<-mean(w) > } else { > out<-mean(data[wt]) > } > return(out) > } > > aaa(mydata,mydata$v1) > aaa(mydata,"v1") # want this call to work ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.