[R] Window on a vector

David Winsemius dwinsemius at comcast.net
Sat Mar 10 14:49:02 CET 2012

On Mar 10, 2012, at 7:44 AM, Alaios wrote:

> Dear all,
> I have a large vector (lets call it myVector) and I want to plot its  
> value with the logic below
> yaxis<-myVector[1]
> yaxis<-c(xaxis,mean(myvector[2:3])
> yaxis<-c(xaxis,mean(myvector[4:8])
> yaxis<c(xaxis,mean(myvector[9:16])
> yaxis<c(xaxis,mean(myvector[17:32])
> this has to stop when the new .....  
> yaxis<c(xaxis,mean(myvector[1024:2048]) will not find the  
> correspondent number of elements, either wise it will stop with an  
> error.
> How I can do something like that in R?

This will generate two series that are somewhat like your index  
specification. I say "somewhat" because you appear to have changed the  
indexing strategy in the middle. You start with 2^0. 2^1 and 2^2 as  
you "begin" but then switch to 2^3+1, and 2^4+1.

cbind(2^(0:(n-1)), 2^(1:n)-1)

You can decide what to use for n with logic like:

which.max(20 >= 2^(1:10) )

Then you can use sapply or mapply.

> Alex
> 	[[alternative HTML version deleted]]

Please learn to post in plain text.


David Winsemius, MD
West Hartford, CT

More information about the R-help mailing list