Thanks! From this I learn the much needed class statement if (class(wt)=="character") wt <- x[, wt] which serves my need in a bigger project. Steven Yen On 6/23/2015 6:20 PM, boB Rudis wrote:> 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 <syen04 at 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 >-- Steven Yen My e-mail alert: https://youtu.be/9UwEAruhyhY?list=PLpwR3gb9OGHP1BzgVuO9iIDdogVOijCtO
Note that objects can have more than one class, in which case your == and %in% might not work as expected. Better to use inherits(). cheers, Steve -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Steven Yen Sent: Wednesday, 24 June 2015 11:37a To: boB Rudis Cc: r-help mailing list Subject: Re: [R] Call to a function Thanks! From this I learn the much needed class statement if (class(wt)=="character") wt <- x[, wt] which serves my need in a bigger project. Steven Yen On 6/23/2015 6:20 PM, boB Rudis wrote:> 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 <syen04 at 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 >-- Steven Yen My e-mail alert: https://youtu.be/9UwEAruhyhY?list=PLpwR3gb9OGHP1BzgVuO9iIDdogVOijCtO ______________________________________________ R-help at 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.
>>>>> Steve Taylor <steve.taylor at aut.ac.nz> >>>>> on Wed, 24 Jun 2015 00:56:26 +0000 writes:> Note that objects can have more than one class, in which case your == and %in% might not work as expected. > Better to use inherits(). > cheers, > Steve Yes indeed, as Steve said, really do! The use of (class(.) == "....") it is error prone and against the philosophy of classes (S3 or S4 or ..) in R : Classes can "extend" other classes or "inherit" from them; S3 examples in "base R" are - glm() objects which are "glm" but also inherit from "lm" - multivariate time-series are "mts" and "ts" - The time-date objects POSIXt , POSIXct, POSIXlt ==> do work with inherits(<obj>, <class)) or possibly is( <obj>, <class>) We've seen this use of class(.) == ".." (or '!=" or %in% ...) in too many places; though it may work fine in your test cases, it is wrong to be used in generality e.g. inside a function you provide for more general use, and is best replaced with the use of inherits() / is() everywhere "out of principle". Martin Maechler ETH Zurich