Skip to content
Prev 56059 / 398500 Next

loops: pasting indexes in variables names

Hi Umberto,

look at ?get, ?assign, ?paste and try the following:

x1 <- rnorm(10)
x2 <- rnorm(10)
y1 <- rnorm(10)
y2 <- rnorm(10)
z1 <- rnorm(10)
z2 <- rnorm(10)
######
names. <- c("x", "y", "z")
for(i in names.){
    res1 <- get(paste(i,"1",sep=""))
    res2 <- get(paste(i,"2",sep=""))
    assign(paste("d",i,sep=""), res1-res2)
}
#######
all.equal(dx, x1-x2)
all.equal(dy, y1-y2)
all.equal(dz, z1-z2)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Umberto Maggiore" <umberto_maggiore at hotmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, September 22, 2004 2:12 PM
Subject: [R] loops: pasting indexes in variables names