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 (Please excuse the bastardized R syntax, but not knowing the R syntax is the reason I am now writing) The goal of this structure is to produce a graph for each unique value of condition which are sequentially numbered and entitled and saved with the condition state as part of the filename/title of the plot. I know this must be simple in R, but the syntax is throwing me. As an aside, could I get recommendations on the best S-Plus/ R book for data manipulation. I have Venables & Ripley, "Modern Applied Statistics with S-Plus" and a smattering of web docs, but nothing seems to go into the details of indexing, built in fuctions, etc that seem pretty important. Thanks in Advance. Michaell Taylor -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 > followingI think you want something like for(state in unique(condition)){ postscript(paste("figure",state,sep=""),height=3,horiz=FALSE) these<-condition %in% state plot(X[these],Y[these],xlab="X",ylab="Y", main=paste("Condition is",state)) dev.off() } The loop could be replaced with sapply(), but I would expect that the file access would be the rate-limiting step so sapply() wouldn''t help a lot. The whole thing could also be done with tapply() or by(). This is left as an exercise for the reader. -thomas Thomas Lumley Asst. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._