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

2020-02-24 Thread Lijun Zhao
Hi Peter, Thank you so much. Kind regards, Lijun -Original Message- From: peter dalgaard Sent: Friday, 21 February 2020 8:16 PM To: Lijun Zhao Cc: William Dunlap ; r-help@r-project.org Subject: Re: [R] How to index the occasions in a vector repeatedly under condition 1? if not, it

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

2020-02-21 Thread William Dunlap via R-help
> all.equal(y, ave(d, cumsum(c(TRUE,is_true(diff(a)!=0))), FUN=function(di)1L+cumsum(is_true(di>15 [1] TRUE Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Feb 19, 2020 at 7:20 PM Lijun Zhao wrote: > Dear William, > > Thank you so much. > > > > I am quiet new in R. I would like to do

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

2020-02-21 Thread peter dalgaard
It has isTRUE, but that is not vectorized, and in fact explicitly tests length==1, so > isTRUE(c(TRUE,FALSE,NA)) [1] FALSE > isTRUE(c(TRUE,TRUE, TRUE)) # I thought I thaw a puddycat... ;-) [1] FALSE > Vectorize(isTRUE)(c(TRUE,FALSE,NA)) [1] TRUE FALSE FALSE (The latter would be silly as an

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

2020-02-20 Thread Lijun Zhao
Dear William, Thank you so much. I am quiet new in R. I would like to do this based on another repeated variables. For example: a<-c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2) d<-c(NA, 0, 0, 0, 8, 0, 577, 69, 0, NA, 0, 0, 0, 8, 0, 577, 69, 0) the outcome I want is : y<-c(1, 1, 1, 1, 1, 1, 2, 3, 3, 1,

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

2020-02-19 Thread William Dunlap via R-help
Use cumsum(logicalVector) to increment a counter at the TRUE positions in logicalVector. . E.g., > d <- c(NA, 0, 0, 0, 8, 0, 577, 69, 0) > is_true <- function(x) !is.na(x) & x > 1 + cumsum( is_true(d >= 15) ) [1] 1 1 1 1 1 1 2 3 3 Some packages have the equivalent of that is_true function,

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

2020-02-19 Thread Lijun Zhao
Dear all, Could you please help me how to get the output as I described in the following example? 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 occasions in x repeatedly if the diff<15? if diff>=15, it will

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

2020-02-19 Thread Rui Barradas
Hello, Yes, or even simpler is to assume that the first group starts at the first element of x, a reasonable assumption. cumsum(c(TRUE, diff(x) > 15)) Hope this helps, Rui Barradas Às 10:36 de 19/02/20, PIKAL Petr escreveu: Hi You could get similar result with using diff function Rui

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

2020-02-19 Thread PIKAL Petr
Hi You could get similar result with using diff function Rui suggested c(1,cumsum((diff(x)>15))+1) [1] 1 1 1 1 1 1 2 3 3 Cheers Petr > -Original Message- > From: R-help On Behalf Of Rui Barradas > Sent: Wednesday, February 19, 2020 8:13 AM > To: Lijun Zhao ; r-help@r-project.org >

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

2020-02-18 Thread Rui Barradas
Hello, First of all, a note about your reproducible example. 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

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

2020-02-18 Thread Lijun Zhao
Dear All, could you please help me how to get the output from the following example? 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