[R] loops: pasting indexes in variables names

Umberto Maggiore umberto_maggiore at hotmail.com
Wed Sep 22 14:12:03 CEST 2004

I cannot figure out how, using R, I can paste indexes or characters to the 
names which are used within loops. I will explain this with a simple 
Immagine I have a huge series of variables, each one taken two times, say
x1 x2 y1 y2 z1 z2.....
Now, immagine that I want to compute a variable from the difference of
each couple, say dx=x1-x2, dy=y1-y2, dz=z1-z2...
In Stata, for example,  this wold be straightforward:
foreach i in x y z   {
		gen  d`i'= `i'1-`i'2
With R I tried to use paste( ) but I found that it applies to objects,
not to variable names.
best regards,

More information about the R-help mailing list