Skip to content

Use of variables to reference to objects

1 message · Liaw, Andy

#
get() is your friend; e.g.

 plot(get(paste("x",1, sep="")), get(paste("x", 2, sep="")))

Andy

-----Original Message-----
From: Kris Nackaerts [mailto:kris.nackaerts at agr.kuleuven.ac.be]
Sent: Friday, November 22, 2002 11:22 AM
To: r-help at stat.math.ethz.ch
Subject: [R] Use of variables to reference to objects


Hi,

I'm new to R and looking for a way to use a variable to reference to an 
object.

I'm plotting several graphs on top of each other and want to do this by 
a for loop. The field I want to graph have names like a1,a2,a3, ...
I can't figure out how to get this working:

It should look like this:
lines(spectral$Wavelength,paste("spectral$a",j,sep=""),col=j)

but the "paste()" does not seem to work in this context.

Any help?

Kind regards,

Kris