Hello, I need your help on this matrix: 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5 5 2 2 2 2 2 2 3 3 3 3 3 3 6 6 6 6 6 6 2 2 2 2 2 2 3 3 3 3 3 3 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 First three rows represent first simulation, next three rows = second simulation etc. I would like to estimate the values in row 1 for example by taking the mean of rows 1, 4, 7, 10. Do same for all other rows. So the resulting matrix is 3-by-6. Thanks Atem. [[alternative HTML version deleted]]
Hi, You probably should read R documentation and learn how to use "seq" ?seq ?rowMeans HTH, Pascal On 05/16/2013 03:17 PM, Zilefac Elvis wrote:> Hello, > > I need your help on this matrix: > > > 1 1 1 1 1 1 > 2 2 2 2 2 2 > 3 3 3 3 3 3 > 5 5 5 5 5 5 > 2 2 2 2 2 2 > 3 3 3 3 3 3 > 6 6 6 6 6 6 > 2 2 2 2 2 2 > 3 3 3 3 3 3 > 1 1 1 1 1 1 > 2 2 2 2 2 2 > 3 3 3 3 3 3 > > First three rows represent first simulation, next three rows = second simulation etc. > I would like to estimate the values in row 1 for example by taking the mean of rows 1, 4, 7, 10. Do same for all other rows. So the resulting matrix is > 3-by-6. > > Thanks > Atem. > [[alternative HTML version deleted]] > > > > ______________________________________________ > 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. >
May be this helps: mat1<- as.matrix(read.table(text=" ?1 1 1 1 1 1 ?2 2 2 2 2 2 ?3 3 3 3 3 3 ?5 5 5 5 5 5 ?2 2 2 2 2 2 ?3 3 3 3 3 3 ?6 6 6 6 6 6 ?2 2 2 2 2 2 ?3 3 3 3 3 3 ?1 1 1 1 1 1 ?2 2 2 2 2 2 ?3 3 3 3 3 3 ",sep="",header=FALSE)) colnames(mat1)<- NULL ?t(sapply(1:3,function(i) colMeans(mat1[seq(i,nrow(mat1),3),]))) #???? [,1] [,2] [,3] [,4] [,5] [,6] #[1,] 3.25 3.25 3.25 3.25 3.25 3.25 #[2,] 2.00 2.00 2.00 2.00 2.00 2.00 #[3,] 3.00 3.00 3.00 3.00 3.00 3.00 A.K. ----- Original Message ----- From: Zilefac Elvis <zilefacelvis at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Thursday, May 16, 2013 2:17 AM Subject: [R] estimate value from simulations Hello, I need your help on this matrix: ? ?1 1 1 1 1 1 ?2 2 2 2 2 2 ?3 3 3 3 3 3? ?5 5 5 5 5 5 ?2 2 2 2 2 2 ?3 3 3 3 3 3? ?6 6 6 6 6 6 ?2 2 2 2 2 2 ?3 3 3 3 3 3? ?1 1 1 1 1 1 ?2 2 2 2 2 2 ?3 3 3 3 3 3? First three rows represent first simulation, next three rows = second simulation etc. I would like to estimate the values in row 1 for example by taking ?the mean ?of rows 1, 4, 7, 10. Do same for all other rows. So the resulting matrix is? 3-by-6. Thanks Atem. ??? [[alternative HTML version deleted]] ______________________________________________ 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.
Hello, Try the following. mat <- matrix(scan(text = " 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5 5 2 2 2 2 2 2 3 3 3 3 3 3 6 6 6 6 6 6 2 2 2 2 2 2 3 3 3 3 3 3 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 "), ncol = 6, byrow = TRUE) idx <- 1 + (seq_len(nrow(mat)) - 1) %% 3 aggregate(mat, list(idx), mean) Hope this helps, Rui Barradas Em 16-05-2013 07:17, Zilefac Elvis escreveu:> Hello, > > I need your help on this matrix: > > > 1 1 1 1 1 1 > 2 2 2 2 2 2 > 3 3 3 3 3 3 > 5 5 5 5 5 5 > 2 2 2 2 2 2 > 3 3 3 3 3 3 > 6 6 6 6 6 6 > 2 2 2 2 2 2 > 3 3 3 3 3 3 > 1 1 1 1 1 1 > 2 2 2 2 2 2 > 3 3 3 3 3 3 > > First three rows represent first simulation, next three rows = second simulation etc. > I would like to estimate the values in row 1 for example by taking the mean of rows 1, 4, 7, 10. Do same for all other rows. So the resulting matrix is > 3-by-6. > > Thanks > Atem. > [[alternative HTML version deleted]] > > > > ______________________________________________ > 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. >