# [R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Feb 19 08:13:20 CET 2020

```Hello,

When you write diff <- x - lag(x) there are two things to be said.

1. There is a base R function named 'diff', it is better to use another
name.

diff(x)
#[1]   0   0   0   8   0 577  69   0

2. There are also several functions named 'lag', one of them in base
package stats.

x - lag(x)
#[1] 0 0 0 0 0 0 0 0 0
#attr(,"tsp")
#[1] 0 8 1

This is not the one you are using.

x - dplyr::lag(x)
#[1]  NA   0   0   0   8   0 577  69   0

start your scripts with library(<pkgname>), in this case library(dplyr).

Now for the question's problem. I will use a different name, 'd', not
'diff'. And qualify the function name with the package name prefix.

The main problem is the NA in the first element of 'd', without it
cumsum(d > 15) would be enough. This works because the logical values
FALSE/TRUE are coded as 0/1 and their cumulative sum goes up every time
a TRUE is found.

d <- x - dplyr::lag(x)
cumsum(is.na(d) | d > 15)
#[1] 1 1 1 1 1 1 2 3 3

Hope this helps,

Às 06:56 de 19/02/20, Lijun Zhao escreveu:
> Dear All,
>
>
>
> x<-c(543,  543,  543,  543,  551 , 551 ,1128 ,1197, 1197)
>
> diff<-x-lag(x)
>
> diff
>
> [1]  NA   0   0   0   8   0 577  69   0
>
> how to index the occassions in x repeatedly if the diff>15? if not, it will
> give a new index
>
> i want the output be like y
>
> y<-c(1,1,1,1,1,1,2,3,3)
>
>
> thanks,
>
>
> Lijun
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help