Suppose I have the following: x1<-as.vector(rnorm(10)) x2<-as.vector(rnorm(10)) x3<-as.vector(rnorm(10)) x4<-as.vector(rnorm(10)) x5<-as.vector(rnorm(10)) x6<-as.vector(rnorm(10)) x7<-as.vector(rnorm(10)) x8<-as.vector(rnorm(10)) x9<-as.vector(rnorm(10)) x10<-as.vector(rnorm(10)) I would like the mean of x1 to x6 for each vector position. I would do something else with x7-x10. These vectors are not currently in a dataframe. I tried to be clever by trying get(paste(paste("x", 1:6, sep=""), collapse="+")) but it didn't work. Any thoughts are greatly appreciated, Pablo -- View this message in context: http://r.789695.n4.nabble.com/Summing-x1-to-x6-tp4214604p4214604.html Sent from the R help mailing list archive at Nabble.com.
Not sure what you mean by mean of each position, so here are two possibilities: rowMeans and colMeans:> x1<-as.vector(rnorm(10)) > x2<-as.vector(rnorm(10)) > x3<-as.vector(rnorm(10)) > x4<-as.vector(rnorm(10)) > x5<-as.vector(rnorm(10)) > x6<-as.vector(rnorm(10)) > x7<-as.vector(rnorm(10)) > x8<-as.vector(rnorm(10)) > x9<-as.vector(rnorm(10)) > x10<-as.vector(rnorm(10)) > > # create matrix > x1.6 <- rbind(x1, x2, x3, x4, x5, x6) > rowMeans(x1.6)x1 x2 x3 x4 x5 x6 -0.7845288 0.3254140 -0.4430038 -0.1533713 0.6074253 -0.1560222> colMeans(x1.6)[1] 0.23480212 -0.08486182 -0.28657007 -0.59151369 0.08995106 0.55138690 -0.22388657 [8] -0.36354109 -0.20111151 -0.13146655>On Mon, Dec 19, 2011 at 11:00 AM, Pablo <pschatfield at gmail.com> wrote:> Suppose I have the following: > > x1<-as.vector(rnorm(10)) > x2<-as.vector(rnorm(10)) > x3<-as.vector(rnorm(10)) > x4<-as.vector(rnorm(10)) > x5<-as.vector(rnorm(10)) > x6<-as.vector(rnorm(10)) > x7<-as.vector(rnorm(10)) > x8<-as.vector(rnorm(10)) > x9<-as.vector(rnorm(10)) > x10<-as.vector(rnorm(10)) > > I would like the mean of x1 to x6 for each vector position. ?I would do > something else with x7-x10. ?These vectors are not currently in a dataframe. > I tried to be clever by trying get(paste(paste("x", 1:6, sep=""), > collapse="+")) but it didn't work. ?Any thoughts are greatly appreciated, > > Pablo > > -- > View this message in context: http://r.789695.n4.nabble.com/Summing-x1-to-x6-tp4214604p4214604.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Dec 19, 2011, at 11:00 AM, Pablo wrote:> Suppose I have the following: > > x1<-as.vector(rnorm(10)) > x2<-as.vector(rnorm(10)) > x3<-as.vector(rnorm(10)) > x4<-as.vector(rnorm(10)) > x5<-as.vector(rnorm(10)) > x6<-as.vector(rnorm(10)) > x7<-as.vector(rnorm(10)) > x8<-as.vector(rnorm(10)) > x9<-as.vector(rnorm(10)) > x10<-as.vector(rnorm(10)) > > I would like the mean of x1 to x6 for each vector position.> apply( data.frame(x1,x2,x3,x4,x5,x6), 1, mean) [1] -0.39321854 -0.92940746 -0.04843466 -0.27764111 0.44058599 0.73512759 [7] -0.22232332 -0.89535287 -0.33430655 0.66217526 > apply( matrix(c(x1,x2,x3,x4,x5,x6),ncol=6), 1, mean) [1] -0.39321854 -0.92940746 -0.04843466 -0.27764111 0.44058599 0.73512759 [7] -0.22232332 -0.89535287 -0.33430655 0.66217526 After seeing Jim Holtman's suggestion, it should be clear that rowMeans would work just even better than these `apply` solutions when used with this "vertical" orientation of the vectors.> I would do > something else with x7-x10. These vectors are not currently in a > dataframe. > I tried to be clever by trying get(paste(paste("x", 1:6, sep=""), > collapse="+")) but it didn't work. Any thoughts are greatly > appreciated, >-- David Winsemius, MD West Hartford, CT
Maybe Matching Threads
- Summarizing elements for a data.frame
- question about converting a matrix to a dataframe
- grep problem decimal points looping
- plot questions?-errors in persp(x1, x2, y) and contour(x1, x2, y)
- Indexing in anova summary output of the form: summary(aov(y ~ x1, Error = (x1/x2)))