Hi, Many apologies for the simplicity (hopefully!) of this request - I can't find it on the forum, but it may have been asked in the past. I have a data frame consisting of ~2000 rows. I simply want to take the average of the first 6, then the next 6, then the next 6 until the end of the table. The command mean(mole[1:6,c("PercentPI")]) gets me the first 6 rows (column is PercentPI), but I don't know how to increase the rows incrementally. Thanks in advance. J -- View this message in context: http://r.789695.n4.nabble.com/Regular-repeats-tp4673653.html Sent from the R help mailing list archive at Nabble.com.
What about something like this: tmp <- data.frame(var1 = rnorm(36), ind = gl(6,6)) with(tmp, tapply(var1, ind, mean)) You can see that your version of mean(tmp[1:6,c("var1")]) gives the same as mine for the first 6 rows. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of jsf1982 Sent: Tuesday, August 13, 2013 12:46 PM To: r-help at r-project.org Subject: [R] Regular repeats Hi, Many apologies for the simplicity (hopefully!) of this request - I can't find it on the forum, but it may have been asked in the past. I have a data frame consisting of ~2000 rows. I simply want to take the average of the first 6, then the next 6, then the next 6 until the end of the table. The command mean(mole[1:6,c("PercentPI")]) gets me the first 6 rows (column is PercentPI), but I don't know how to increase the rows incrementally. Thanks in advance. J -- View this message in context: http://r.789695.n4.nabble.com/Regular-repeats-tp4673653.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.
On 13-08-2013, at 18:46, jsf1982 <jamie.freeman at ucl.ac.uk> wrote:> Hi, > Many apologies for the simplicity (hopefully!) of this request - I can't > find it on the forum, but it may have been asked in the past. > > I have a data frame consisting of ~2000 rows. I simply want to take the > average of the first 6, then the next 6, then the next 6 until the end of > the table. > The command > > mean(mole[1:6,c("PercentPI")]) > > gets me the first 6 rows (column is PercentPI), but I don't know how to > increase the rows incrementally.Something like this N <- 27 dd <- data.frame(A=rnorm(1:N),index=gl(6,6,N)) aggregate(dd$A,by=list(dd$index),mean) Berend
Hi, You could try: set.seed(24) ? dat1<- as.data.frame(matrix(sample(1:50,29*6,replace=TRUE),ncol=6)) ((seq_len(nrow(dat1))-1)%/%6)+1 # [1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 #For a particular column: aggregate(dat1[,5],list(((seq_len(nrow(dat1))-1)%/%6)+1),FUN=mean) #? Group.1??????? x #1?????? 1 38.16667 #2?????? 2 29.50000 #3?????? 3 23.16667 #4?????? 4 21.16667 #5?????? 5 20.60000 #or for the whole columns aggregate(dat1,list(((seq_len(nrow(dat1))-1)%/%6)+1),FUN=mean) #? Group.1?????? V1?????? V2?????? V3?????? V4?????? V5?????? V6 #1?????? 1 28.33333 17.50000 12.66667 35.00000 38.16667 30.16667 #2?????? 2 26.16667 31.33333 35.33333 19.66667 29.50000 24.83333 #3?????? 3 24.00000 11.83333 20.00000 25.50000 23.16667 20.83333 #4?????? 4 18.33333 23.33333 23.66667 20.33333 21.16667 21.16667 #5?????? 5 22.60000 30.40000 17.40000 21.80000 20.60000 24.40000 #or library(plyr) res1<-ddply(dat1,.(((seq_len(nrow(dat1))-1)%/%6)+1),summarize,MeanV1=mean(V1)) ?colnames(res1)[1]<-"Group" res1 #? Group?? MeanV1 #1???? 1 28.33333 #2???? 2 26.16667 #3???? 3 24.00000 #4???? 4 18.33333 #5???? 5 22.60000 A.K. ----- Original Message ----- From: jsf1982 <jamie.freeman at ucl.ac.uk> To: r-help at r-project.org Cc: Sent: Tuesday, August 13, 2013 12:46 PM Subject: [R] Regular repeats Hi, Many apologies for the simplicity (hopefully!) of this request - I can't find it on the forum, but it may have been asked in the past. I have a data frame consisting of ~2000 rows. I simply want to take the average of the first 6, then the next 6, then the next 6 until the end of the table. The command mean(mole[1:6,c("PercentPI")]) gets me the first 6 rows (column is PercentPI), but I don't know how to increase the rows incrementally. Thanks in advance. J -- View this message in context: http://r.789695.n4.nabble.com/Regular-repeats-tp4673653.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.