[R] Looping syntax?

Claudia Tebaldi tebaldi at rap.ucar.edu
Fri Jun 29 21:47:45 CEST 2001


On Fri, 29 Jun 2001, Michaell Taylor wrote:

> 
> 
> Sorry for what I hope is an unchallenging question for R veterans.
> 
> I have a question regarding looping syntax, though I know this should be 
> avoided if possible in R - as in most other stat languages with which I am 
> familiar.  
> 
> I would like to perform a series of operations on the subsets as defined by 
> condition. Given the following data frame 
> 
> condition	X 	Y
> aa		4	5
> aa		8	9
> .
> .
> bb		8	4
> bb		3 	9
> . 
> . 
> more ..
> 
> There are over 100 condition states, each with several hundred observations.  
> For ease of this example, lets assume that I wish to make XY plots of each of 
> the 100 condition states, with In other words, I need a structure like the 
> following
> 
> for state in condition:
> 	identifier <- c("figure N:", cond)
> 	postscript(identifier,height=3,width=6,horizontal=F) 
> 	plot(X,Y, main=indentifier)
> 	n <- n+1
> 

say your dataframe is called pippo and its columns are called 
condition, X, Y by

names(pippo)_c("condition","X",Y")


then 


 unique.cond_unique(pippo$condition) 
 l_length(unique.cond)  

 for(i in 1:l){
 filename_paste("figure_",unique.cond[i],".ps",sep="")  #will be 
                                                        #something
                                                       #like figure_aa.ps
 fig.title_paste("figure N:",unique.cond[i])
 postscript(file=filename,height=3, width=6, hor=T)
 plot(cbind(pippo$X,pippo$Y)[pippo$condition==unique.cond[i],],
	main=fig.title,xlab="X", ylab="Y") 
 dev.off()

}

this gives you as many files as there are unique conditions.


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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