4

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

Marek
  • 49,472
  • 15
  • 99
  • 121
Misha
  • 3,114
  • 8
  • 39
  • 60
  • 1
    In fourth line should be `b<-(w%in%a2|w%in%a7)` or `b<-w%in%c(a2,a7)`. And you use `A` as argument and `a` in code. – Marek Jun 01 '10 at 10:46
  • 1
    This question could be cleaned up. The example code is much too specific to one user’s needs and includes too much irrelevant noise. – randy Jun 05 '22 at 00:10

2 Answers2

5

You should probably choose a different name for your function other than "q" - otherwise you'll never be able to finish ;)

mdsumner
  • 29,099
  • 6
  • 83
  • 91
4

You can use get

For instance

var1 <- get(paste(e, ".2", sep=""))
var2 <- get(paste(e, ".7", sep=""))

EDIT: as Aidan Cully correctly says then you should call your function as q("e") (i.e. with a string)

nico
  • 50,859
  • 17
  • 87
  • 112