Ugh...This should be very simple, but evidently I am not searching for the proper term. Given the below: val_size<-100000 x_vals<-rnorm(val_size) I would like to group them according to the following x_vals_mean_tmp[1]<-mean(x_vals[1:10]) x_vals_mean_tmp[2]<-mean(x_vals[11:20]) ... x_vals_mean_tmp[n]<-mean(x_vals[99991:100000]) Then, I would like to group them according to the following x_vals_mean_tmp[1]<-mean(x_vals[1:100]) x_vals_mean_tmp[2]<-mean(x_vals[101:200]) ... x_vals_mean_tmp[m]<-mean(x_vals[99901:100000]) etc. I'm pretty sure I can come up with a loop to do this, but wondering if there is something that will allow me to break up the x_vals vector according to a certain step size. I looked at split and cut, but those did not appear to accomplish what is needed. Thanks again.
Look at the seq function's help page. -- David Winsemius On Mar 24, 2009, at 10:52 PM, Jason Rupert wrote:> > Ugh...This should be very simple, but evidently I am not searching > for the proper term. > > Given the below: > val_size<-100000 > x_vals<-rnorm(val_size) > > I would like to group them according to the following > x_vals_mean_tmp[1]<-mean(x_vals[1:10]) > x_vals_mean_tmp[2]<-mean(x_vals[11:20]) > ... > x_vals_mean_tmp[n]<-mean(x_vals[99991:100000]) > > Then, > I would like to group them according to the following > x_vals_mean_tmp[1]<-mean(x_vals[1:100]) > x_vals_mean_tmp[2]<-mean(x_vals[101:200]) > ... > x_vals_mean_tmp[m]<-mean(x_vals[99901:100000]) > etc. > > I'm pretty sure I can come up with a loop to do this, but wondering > if there is something that will allow me to break up the x_vals > vector according to a certain step size. I looked at split and cut, > but those did not appear to accomplish what is needed. > > Thanks again.Heritage Laboratories West Hartford, CT
Dear Jason, Try this: # First case N<-100000 X<-rnorm(N) # Groups g<-rep(1:(N/n),each=10) # The result tapply(X,g,mean) For the second case, just change each=10 by each=100 and run again the code above. HTH, Jorge On Tue, Mar 24, 2009 at 10:52 PM, Jason Rupert <jasonkrupert@yahoo.com>wrote:> > Ugh...This should be very simple, but evidently I am not searching for the > proper term. > > Given the below: > val_size<-100000 > x_vals<-rnorm(val_size) > > I would like to group them according to the following > x_vals_mean_tmp[1]<-mean(x_vals[1:10]) > x_vals_mean_tmp[2]<-mean(x_vals[11:20]) > ... > x_vals_mean_tmp[n]<-mean(x_vals[99991:100000]) > > Then, > I would like to group them according to the following > x_vals_mean_tmp[1]<-mean(x_vals[1:100]) > x_vals_mean_tmp[2]<-mean(x_vals[101:200]) > ... > x_vals_mean_tmp[m]<-mean(x_vals[99901:100000]) > etc. > > I'm pretty sure I can come up with a loop to do this, but wondering if > there is something that will allow me to break up the x_vals vector > according to a certain step size. I looked at split and cut, but those did > not appear to accomplish what is needed. > > Thanks again. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]