[R] how to find interval?
Berend Hasselman
bhh at xs4all.nl
Tue Sep 17 12:41:31 CEST 2013
On 17-09-2013, at 12:15, gildororonar at mail-on.us wrote:
> I can do this:
>
>> 1:10 %in% c(3, 5, 6, 10)
> [1] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE TRUE
>
> but what I wish to get is:
>
> [1] 3 2 1 4
>
> let me explain:
>
> 3 # [1:3] ends with TRUE, i.e. FALSE FALSE TRUE
> 2 # [4:5] ends with TRUE, i.e. FALSE TRUE
> 1 # [6:6] ends with TRUE, i.e. TRUE
> 4 # [7:10] ends with TRUE, i.e. FALSE FALSE FALSE TRUE
>
> That is, %in% gave me a serial whether or not the element is in a set, the length is equal to the former, i.e. 1:10
>
> But I wish to get a serial of intevals of occurance of the element in the set, the length is equal to the latter i.e. c(3, 5, 6, 10)
One way is
> diff(c(0,which(1:10 %in% a)))
[1] 3 2 1 4
Berend
More information about the R-help
mailing list