Hello, I've got an array defined as y <- rnorm(3000), dim(y) <- c(3, 1000). I'd like to produce a 1000-element vector z that is the mean of the corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], y[3,1])), but being new to R, I'm not sure how to do this for all elements at once (or, at least, simply). Any help is appreciated. Thanks, Tom
On Mon, 2005-01-10 at 22:45 -0500, Thomas Hopper wrote:> Hello, > > I've got an array defined as y <- rnorm(3000), dim(y) <- c(3, 1000). > > I'd like to produce a 1000-element vector z that is the mean of the > corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], > y[3,1])), but being new to R, I'm not sure how to do this for all > elements at once (or, at least, simply). Any help is appreciated. > > Thanks, > > Tom# You can create 'y' in one step here y <- matrix(rnorm(3000), ncol = 1000) # get the column means z <- colMeans(y)> str(z)num [1:1000] -0.5664 0.8232 -0.0138 -0.5511 1.0224 ... See ?colMeans for more info. HTH, Marc Schwartz
Tom, I often use a loop, such as:>m <- rep(0,1000) > for (i in 1:1000)+ m[i]<-mean(y[,i])> m[1:20][1] -0.04914724 -0.28253861 -0.31112690 -0.18034371 0.18839167 0.66448244 [7] 0.19769017 -1.28363405 -0.05167451 -0.95492534 -1.23285174 0.10288562 [13] -0.73792584 -0.19297468 -0.59059036 -0.11870173 0.38285449 1.19154411 [19] 0.34663980 0.21322554 But there may be more efficient ways to accomplish this. Tim ---- Original message ---->Date: Mon, 10 Jan 2005 22:45:56 -0500 >From: Thomas Hopper <thopper106035 at comcast.net> >Subject: [R] Calculate Mean of Column Vectors? >To: r-help at stat.math.ethz.ch > >Hello, > >I've got an array defined as y <- rnorm(3000), dim(y) <- c(3,1000).> >I'd like to produce a 1000-element vector z that is the meanof the>corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], >y[3,1])), but being new to R, I'm not sure how to do this forall>elements at once (or, at least, simply). Any help is appreciated. > >Thanks, > >Tom > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html
apply(y,2,mean) Is this what you are after. If it is I would suggest that you look at the examples not just for this but for what I call the apply family sapply, tapply, mapply. Once you get the hang of these they are really helpful. Tom.> -----Original Message----- > From: Thomas Hopper [mailto:thopper106035 at comcast.net] > Sent: Tuesday, 11 January 2005 11:46 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Calculate Mean of Column Vectors? > > > Hello, > > I've got an array defined as y <- rnorm(3000), dim(y) <- c(3, 1000). > > I'd like to produce a 1000-element vector z that is the mean of the > corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], > y[3,1])), but being new to R, I'm not sure how to do this for all > elements at once (or, at least, simply). Any help is appreciated. > > Thanks, > > Tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
z <- apply(y, 2, mean) Cheers, Simon.>Hello, > >I've got an array defined as y <- rnorm(3000), dim(y) <- c(3, 1000). > >I'd like to produce a 1000-element vector z that is the mean of the >corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], >y[3,1])), but being new to R, I'm not sure how to do this for all >elements at once (or, at least, simply). Any help is appreciated. > >Thanks, > >Tom > >______________________________________________ >R-help@stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat. Visiting Fellow School of Botany & Zoology The Australian National University Canberra ACT 0200 Australia T: +61 2 6125 8057 email: Simon.Blomberg@anu.edu.au F: +61 2 6125 5573 CRICOS Provider # 00120C [[alternative HTML version deleted]]
Have you considered the following: apply(y, 1, mean) Alternatively: y.means <- rep(NA, 3) for(i in 1:3) y.means[i] <- mean(y[i,]) hope this helps. spencer graves Thomas Hopper wrote:> Hello, > > I've got an array defined as y <- rnorm(3000), dim(y) <- c(3, 1000). > > I'd like to produce a 1000-element vector z that is the mean of the > corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], > y[3,1])), but being new to R, I'm not sure how to do this for all > elements at once (or, at least, simply). Any help is appreciated. > > Thanks, > > Tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
See ?apply. Example y <- rnorm(3000), dim(y) <- c(3, 1000) my <- apply(y, MARGIN=2, FUN=mean, na.rm=TRUE) to calculate the mean over the 2nd index, that is, for each column, and pass argument na.rm=TRUE to each call to mean(). This is in practice the same as my <- rep(NA, ncol(y)) for (kk in 1:ncol(y)) my[kk] <- mean(y[,kk], na.rm=TRUE) but apply() is to prefer when you get used to it. Best wishes Henrik Bengtsson> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Thomas Hopper > Sent: Tuesday, January 11, 2005 4:46 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Calculate Mean of Column Vectors? > > > Hello, > > I've got an array defined as y <- rnorm(3000), dim(y) <- c(3, 1000). > > I'd like to produce a 1000-element vector z that is the mean of the > corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], > y[3,1])), but being new to R, I'm not sure how to do this for all > elements at once (or, at least, simply). Any help is appreciated. > > Thanks, > > Tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Dear Thomas, My suggestion would be as follows: y <- rnorm(3000) dim(y) <- c(3, 1000). #then create three column vectors out of y using cbind(): w<-cbind(y[1,],y[2,],y[3,]) # and calculate the row means for (i in 1:1000) z[i]<-mean(w[i,1:3]) z Hope I got it right! Regards, Christoph Thomas Hopper wrote:> Hello, > > I've got an array defined as y <- rnorm(3000), dim(y) <- c(3, 1000). > > I'd like to produce a 1000-element vector z that is the mean of the > corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1], > y[3,1])), but being new to R, I'm not sure how to do this for all > elements at once (or, at least, simply). Any help is appreciated. > > Thanks, > > Tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Many helpful replies; my thanks to all of you! colMeans() or the apply() function were what I was looking for (though the looping functions will surely come in handy elsewhere). On Jan 10, 2005, at 11:03 PM, Marc Schwartz wrote:> # get the column means > z <- colMeans(y) >Best regards, Tom