[R] collaspe into a name

Thomas Lumley tlumley at u.washington.edu
Fri Apr 6 18:52:42 CEST 2001


On Fri, 6 Apr 2001, Erin Hodgess wrote:

> Dear R People:
>
> Suppose I have the following:
>
> z <- sample(1:4)
>
> if(z == 1)
> 	bx <- x1
> if(z == 2)
> 	bx <- x2
>
> and so on.
>
> There is something that I can do (Maybe with paste and assign)
> to get
> bx <- paste("x",z,sep=""))
> #Not right, but this is the example.
>
> R version 1.2.2 for Windows
> Could someone please help?

This sort of question comes up fairly frequently. There are two parts to
the solution
 1. You could do
    bx<-get(paste("x",z,sep=""))

 2. This sort of solution usually means you are asking the wrong question.
It is rare that manipulating R commands as text strings is a good
solution. You would probably be better off with x being a list or array so
you could do eg

     x<-list(x1,x2,x3,x4)
     bx<-x[[z]]



	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list