[R] Call to a function

Steven Yen syen04 at gmail.com
Wed Jun 24 01:37:03 CEST 2015


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



More information about the R-help mailing list