Dear list, I have a list containing parameters (time and X1), and have "n" similar data set like the following:> cal[[1]] time X1 1 0.0 10.006306 2 0.5 9.433443 3 1.0 8.893405 4 2.0 7.904274 5 4.0 6.243807 6 6.0 4.932158 7 8.0 3.896049 8 10.0 3.077604 [[2]] time X1 1 0.0 10.015972 2 0.5 9.460064 3 1.0 8.935039 4 2.0 7.970755 5 4.0 6.343151 6 6.0 5.047900 7 8.0 4.017131 8 10.0 3.196856 [[3]] time X1 1 0.0 9.985741 2 0.5 9.552583 3 1.0 9.138239 4 2.0 8.362664 5 4.0 7.003394 6 6.0 5.865057 7 8.0 4.911747 8 10.0 4.113382 [[4]] ....... [[n]] ....... And I would like to put all X1( when time=0) together, time=0.5,1... are the same. then calculate the mean value.> a<-list() > b<-list() > c<-list() > d<-list() > e<-list().......> for(i in 1:n){+ a[[i]]<-cal[[i]][1,2] + b[[i]]<-cal[[i]][2,2] + c[[i]]<-cal[[i]][3,2] + d[[i]]<-cal[[i]][4,2] + e[[i]]<-cal[[i]][5,2] + ......... }>mean.a<-(a[[1]][1]+a[[2]][1]+a[[3]][1]+.....)/n >mean.b<-(b[[1]][1]+b[[2]][1]+b[[3]][1]+.....)/n >mean.c<-(c[[1]][1]+c[[2]][1]+c[[3]][1]+.....)/n >mean.d<-(d[[1]][1]+d[[2]][1]+d[[3]][1]+.....)/n >............. >xy<-c(mean.a,mean.b,mean.c,mean.d,........)But the way I use seem not very smart. So please give me some hints to the simplify this. Thanks in advance !! Sincerely!!
I suggest that when displaying test data in a post that you do it like this: dput(cal) since then others can simply copy and paste it into their session. At any rate, using this test data: cal <- list(A = data.frame(time = 1:3, X1 = 1:3), B = data.frame(1:3, X1 = 3:5)) Pick off the second element of each list component, turn the result into a data frame and take the row means: rowMeans(as.data.frame(lapply(cal, "[", 2))) Since the times are irregular, you might prefer to represent cal as a multivariate zoo object using the zoo library: library(zoo) cal.zoo <- do.call("merge", lapply(cal, function(x) zoo(x[,2], x[,1]))) Once you have done this and other operations simplify. For this one its just: rowMeans(cal.zoo) or to plot them all plot(cal.zoo) # separate plots plot(call.zoo, plot.type = "single") # all on one plot On 12/7/05, Rhett Eckstein <glaxowell at gmail.com> wrote:> Dear list, > I have a list containing parameters (time and X1), and have "n" > similar data set like > the following: > > cal > [[1]] > time X1 > 1 0.0 10.006306 > 2 0.5 9.433443 > 3 1.0 8.893405 > 4 2.0 7.904274 > 5 4.0 6.243807 > 6 6.0 4.932158 > 7 8.0 3.896049 > 8 10.0 3.077604 > > [[2]] > time X1 > 1 0.0 10.015972 > 2 0.5 9.460064 > 3 1.0 8.935039 > 4 2.0 7.970755 > 5 4.0 6.343151 > 6 6.0 5.047900 > 7 8.0 4.017131 > 8 10.0 3.196856 > > [[3]] > time X1 > 1 0.0 9.985741 > 2 0.5 9.552583 > 3 1.0 9.138239 > 4 2.0 8.362664 > 5 4.0 7.003394 > 6 6.0 5.865057 > 7 8.0 4.911747 > 8 10.0 4.113382 > > [[4]] > ....... > > [[n]] > ....... > > And I would like to put all X1( when time=0) together, time=0.5,1... > are the same. > then calculate the mean value. > > a<-list() > > b<-list() > > c<-list() > > d<-list() > > e<-list() > ....... > > for(i in 1:n){ > + a[[i]]<-cal[[i]][1,2] > + b[[i]]<-cal[[i]][2,2] > + c[[i]]<-cal[[i]][3,2] > + d[[i]]<-cal[[i]][4,2] > + e[[i]]<-cal[[i]][5,2] > + ......... > } > >mean.a<-(a[[1]][1]+a[[2]][1]+a[[3]][1]+.....)/n > >mean.b<-(b[[1]][1]+b[[2]][1]+b[[3]][1]+.....)/n > >mean.c<-(c[[1]][1]+c[[2]][1]+c[[3]][1]+.....)/n > >mean.d<-(d[[1]][1]+d[[2]][1]+d[[3]][1]+.....)/n > >............. > >xy<-c(mean.a,mean.b,mean.c,mean.d,........) > But the way I use seem not very smart. > So please give me some hints to the simplify this. > Thanks in advance !! > Sincerely!! > > ______________________________________________ > 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 >
assuming that you have the same number of measurements in each sub-data.frame, you could use something like: cal <- lapply(1:10, function(x) data.frame(time = c(0, 0.5, 1, 2, 4, 6, 8, 10), X1 = rnorm(8, 10:3))) ############## rowMeans(as.data.frame(lapply(cal, "[", "X1"))) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Rhett Eckstein" <glaxowell at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, December 07, 2005 3:38 PM Subject: [R] How to simplify> Dear list, > I have a list containing parameters (time and X1), and have "n" > similar data set like > the following: >> cal > [[1]] > time X1 > 1 0.0 10.006306 > 2 0.5 9.433443 > 3 1.0 8.893405 > 4 2.0 7.904274 > 5 4.0 6.243807 > 6 6.0 4.932158 > 7 8.0 3.896049 > 8 10.0 3.077604 > > [[2]] > time X1 > 1 0.0 10.015972 > 2 0.5 9.460064 > 3 1.0 8.935039 > 4 2.0 7.970755 > 5 4.0 6.343151 > 6 6.0 5.047900 > 7 8.0 4.017131 > 8 10.0 3.196856 > > [[3]] > time X1 > 1 0.0 9.985741 > 2 0.5 9.552583 > 3 1.0 9.138239 > 4 2.0 8.362664 > 5 4.0 7.003394 > 6 6.0 5.865057 > 7 8.0 4.911747 > 8 10.0 4.113382 > > [[4]] > ....... > > [[n]] > ....... > > And I would like to put all X1( when time=0) together, > time=0.5,1... > are the same. > then calculate the mean value. >> a<-list() >> b<-list() >> c<-list() >> d<-list() >> e<-list() > ....... >> for(i in 1:n){ > + a[[i]]<-cal[[i]][1,2] > + b[[i]]<-cal[[i]][2,2] > + c[[i]]<-cal[[i]][3,2] > + d[[i]]<-cal[[i]][4,2] > + e[[i]]<-cal[[i]][5,2] > + ......... > } >>mean.a<-(a[[1]][1]+a[[2]][1]+a[[3]][1]+.....)/n >>mean.b<-(b[[1]][1]+b[[2]][1]+b[[3]][1]+.....)/n >>mean.c<-(c[[1]][1]+c[[2]][1]+c[[3]][1]+.....)/n >>mean.d<-(d[[1]][1]+d[[2]][1]+d[[3]][1]+.....)/n >>............. >>xy<-c(mean.a,mean.b,mean.c,mean.d,........) > But the way I use seem not very smart. > So please give me some hints to the simplify this. > Thanks in advance !! > Sincerely!! > > ______________________________________________ > 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 >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi changing list to matrix and making summary could do the trick lll <- list(a=cbind(1:10, rnorm(10)), b=cbind(1:10, rnorm(10))) mat <- do.call("rbind", lll) tapply(mat[,2], mat[,1], mean) BTW I found a suitable thread with similar question in CRAN search list summary mean HTH Petr On 7 Dec 2005 at 22:38, Rhett Eckstein wrote: Date sent: Wed, 7 Dec 2005 22:38:56 +0800 From: Rhett Eckstein <glaxowell at gmail.com> To: r-help at stat.math.ethz.ch Subject: [R] How to simplify> Dear list, > I have a list containing parameters (time and X1), and have "n" > similar data set like > the following: > > cal > [[1]] > time X1 > 1 0.0 10.006306 > 2 0.5 9.433443 > 3 1.0 8.893405 > 4 2.0 7.904274 > 5 4.0 6.243807 > 6 6.0 4.932158 > 7 8.0 3.896049 > 8 10.0 3.077604 > > [[2]] > time X1 > 1 0.0 10.015972 > 2 0.5 9.460064 > 3 1.0 8.935039 > 4 2.0 7.970755 > 5 4.0 6.343151 > 6 6.0 5.047900 > 7 8.0 4.017131 > 8 10.0 3.196856 > > [[3]] > time X1 > 1 0.0 9.985741 > 2 0.5 9.552583 > 3 1.0 9.138239 > 4 2.0 8.362664 > 5 4.0 7.003394 > 6 6.0 5.865057 > 7 8.0 4.911747 > 8 10.0 4.113382 > > [[4]] > ....... > > [[n]] > ....... > > And I would like to put all X1( when time=0) together, time=0.5,1... > are the same. then calculate the mean value. > a<-list() > b<-list() > > c<-list() > d<-list() > e<-list() ....... > for(i in 1:n){ + > a[[i]]<-cal[[i]][1,2] + b[[i]]<-cal[[i]][2,2] + > c[[i]]<-cal[[i]][3,2] + d[[i]]<-cal[[i]][4,2] + > e[[i]]<-cal[[i]][5,2] + ......... } > >mean.a<-(a[[1]][1]+a[[2]][1]+a[[3]][1]+.....)/n > >mean.b<-(b[[1]][1]+b[[2]][1]+b[[3]][1]+.....)/n > >mean.c<-(c[[1]][1]+c[[2]][1]+c[[3]][1]+.....)/n > >mean.d<-(d[[1]][1]+d[[2]][1]+d[[3]][1]+.....)/n >............. > >xy<-c(mean.a,mean.b,mean.c,mean.d,........) But the way I use seem > not very smart. So please give me some hints to the simplify this. > Thanks in advance !! Sincerely!! > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz