[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@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.
Re: [R] Regular repeats
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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of jsf1982 Sent: Tuesday, August 13, 2013 12:46 PM To: r-help@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@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. __ R-help@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.
Re: [R] Regular repeats
On 13-08-2013, at 18:46, jsf1982 jamie.free...@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 __ R-help@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.
Re: [R] Regular repeats
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.5 #3 3 23.16667 #4 4 21.16667 #5 5 20.6 #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.3 17.5 12.7 35.0 38.16667 30.16667 #2 2 26.16667 31.3 35.3 19.7 29.5 24.8 #3 3 24.0 11.8 20.0 25.5 23.16667 20.8 #4 4 18.3 23.3 23.7 20.3 21.16667 21.16667 #5 5 22.6 30.4 17.4 21.8 20.6 24.4 #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.3 #2 2 26.16667 #3 3 24.0 #4 4 18.3 #5 5 22.6 A.K. - Original Message - From: jsf1982 jamie.free...@ucl.ac.uk To: r-help@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@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. __ R-help@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.