[R] Window on a vector

R. Michael Weylandt michael.weylandt at gmail.com
Mon Mar 12 21:03:42 CET 2012


As the error message show, the problem isn't in the subscripting but
rather in passing a NA to `:`

If you try to implement Dr. Winsemius' method you should be fine.

Also, send plain text email.

Michael

On Mon, Mar 12, 2012 at 11:58 AM, Alaios <alaios at yahoo.com> wrote:
> You will find here a reprodusible example, and you will also see it
> "exploding"
>
>
> myData<-seq(1:100000)
> windowList<-list(1,2,3,4)
> totalLength<- length(myData)
> windowSize<-30
>
> for (i in 2:length(windowList)) {
>     myMean<-array()
>     indexes<-seq(1,totalLength,by=windowSize*windowList[[i]])
>     for (j in c(1,length(indexes))){
>         myMean[j]<-mean(myData[indexes[j]:indexes[j+1]]) # Select windowSize
> number of elements
>         # Then I will call a cdf and cast lines()
>     }
> }
>
> as you can see on a given vector I want to calculating the mean value by
> taking each time different number of elements. The reason is I want to see
> how much increasing the number of samples affects the cdf distribution
>
> I would like to thank you in advance for your help
>
> B.R
> Alex
>
> ________________________________
> From: R. Michael Weylandt <michael.weylandt at gmail.com>
> To: Alaios <alaios at yahoo.com>
> Cc: R help <R-help at r-project.org>
> Sent: Monday, March 12, 2012 4:35 PM
> Subject: Re: [R] Window on a vector
>
> It's not always true that going out of bounds in subscripting gives an
> error (as you seem to assert in your original post)
>
> x <- 1:3
> x[4] # No error
>
> and note that mean() has a na.rm argument.
>
> Perhaps you should construct a *reproducible* example of what you
> think will go wrong.
>
> Michael
>
> On Mon, Mar 12, 2012 at 11:22 AM, Alaios <alaios at yahoo.com> wrote:
>> Yep I did some mistake while I was typing the example
>> What I want is this one
>> cbind(2^(0:(n-1))) and  the problem is that at the last "window" this is
>> going to explode are there will be less elements than what the window asks
>> for.
>>
>> How should I deal with that?
>> Regards
>> Alex
>>
>>
>>
>> ________________________________
>>  From: David Winsemius <dwinsemius at comcast.net>
>>
>> Cc: R help <R-help at r-project.org>
>> Sent: Saturday, March 10, 2012 2:49 PM
>> Subject: Re: [R] Window on a vector
>>
>>
>> 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.
>
> This doesn't seem true:
>
> x <- 1:3
>
> x[4] # Not 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.
>>
>> n=20
>> 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
>>        [[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.
>>
>
>



More information about the R-help mailing list