2011年4月7日星期四

for loop in R

R Language Definition
http://cran.r-project.org/doc/manuals/R-lang.html


The syntax of the for loop is

for ( name in vector )
statement1

where vector can be either a vector or a list. For each element in vector the variable name is set to the value of that element and statement1 is evaluated. A side effect is that the variable name still exists after the loop has concluded and it has the value of the last element of vector that the loop was evaluated for.

################################################################
# an example

you have 60 DNAcopy object you need to plot. The
following code will do the trick:

The first step is to create a vector with the name of the 60 objects:

object.list <- c("DNAcopy1", "DNAcopy2"..."DNAcopy60")

than do a loop to plot them as pdf file:

for (I in object.list) {
pdf(file=paste(I, ".pdf", sep=""), height=10, width=10)
plot(I, plot.type="w")
dev.off()
}

没有评论:

发表评论