Pass variable name to a function in r

Posted by Misha on Stack Overflow See other posts from Stack Overflow or by Misha
Published on 2010-06-01T08:51:22Z Indexed on 2010/06/01 10:43 UTC
Read the original article Hit count: 183

Filed under:
|
|

Is it possible to pass just a variable name in a function call and have it utilised as such within the function??

pseudocode:

q<-function(A){
    b<-(w%in%A.2|w%in%A.7)  
    factor(b,levels=c(F,T),labels=c("non-"A,A))}


w<-c(0:10)
e.2<-c(1,2)
e.7<-c(6,7)

what I´d like to do is

q(e)

and have returned

non-e,e,e,non-e,non-e,e,e,non-e,non-e

//M


q<-function(A) {
    a2<-get(paste(a,".2",sep=""))
    a7<-get(paste(a,".7",sep=""))
    b<-(w%in%a2|%in%a7) 
    factor(b,levels=c(F,T),labels=c(paste("non-",a,sep=""),a)) 
}

q("e")

Thx,

M

© Stack Overflow or respective owner

Related posts about function

Related posts about r