Hi all, I would like to apply a function to each column of an 2-dimensional array, and store the result in a new 1-dimensional vector. I am not sure how to go about doing that syntatically. For instance, can I use lapply? And, if so, how do I specify which dimension should be used? Also, how do I pre-specify the type of object that will go into the 1-dimensional vector. I'm not sure if it is important, the function I wish to apply to the columns is density(). Additionally, I wish to be able to take these density objects (stored in that 1-dim vector) and place them onto a multiple-figure environment. I'm new with R, so I tend to think in loops. I'd appreciate any guidance or tips on better ways to handle this problem. Thanks! Paul -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Use the 'apply' function. For information see ?apply. To plot the density of each column of a matrix, you can do this mat <- matrix( rnorm(100 * 12), ncol=12 ) par(mfrow=c(3,4)) density.list <- apply( mat, 2, density) sapply( density.list, plot ) -Greg> -----Original Message----- > From: Paul Boutros [mailto:pcboutro at engmail.uwaterloo.ca] > Sent: Tuesday, October 29, 2002 3:33 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Apply function to column of array > > > Hi all, > > I would like to apply a function to each column of an > 2-dimensional array, > and store the result in a new 1-dimensional vector. I am not > sure how to > go about doing that syntatically. For instance, can I use > lapply? And, > if so, how do I specify which dimension should be used? > Also, how do I > pre-specify the type of object that will go into the > 1-dimensional vector. > > I'm not sure if it is important, the function I wish to apply to the > columns is density(). Additionally, I wish to be able to take these > density objects (stored in that 1-dim vector) and place them onto a > multiple-figure environment. > > I'm new with R, so I tend to think in loops. I'd appreciate > any guidance > or tips on better ways to handle this problem. > > Thanks! > Paul > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._._._._._._._ >LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
It seems that for this you may in fact want to use a loop. If you have a matrix with p columns (say it's called 'm'), then you might consider: results <- vector("list", length = p) for(i in 1:p) results[[i]] <- density(m[,i]) If you want to use lapply() then something like the following should work: results <- lapply(1:p, function(i) density(m[,i])) Then 'results' is a vector of lists which contain the results to the individual calls to density(). You can then cycle over this vector to make your plots or whatever. -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Tue, 29 Oct 2002, Paul Boutros wrote:> Hi all, > > I would like to apply a function to each column of an 2-dimensional array, > and store the result in a new 1-dimensional vector. I am not sure how to > go about doing that syntatically. For instance, can I use lapply? And, > if so, how do I specify which dimension should be used? Also, how do I > pre-specify the type of object that will go into the 1-dimensional vector. > > I'm not sure if it is important, the function I wish to apply to the > columns is density(). Additionally, I wish to be able to take these > density objects (stored in that 1-dim vector) and place them onto a > multiple-figure environment. > > I'm new with R, so I tend to think in loops. I'd appreciate any guidance > or tips on better ways to handle this problem. > > Thanks! > Paul > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._