Lijun Zhao
2020-Feb-19 06:51 UTC
[R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.
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 give a new index. I want the output be like y. y<-c(1,1,1,1,1,1,2,3,3) Thank you so much, Lijun Zhao (PhD Candidate) Nutrition and Metabolism Level 7 SAHMRI North Terrace Adelaide 5005 Ph : +61 8 8128 4898 e-mail: lijun.zhao at adelaide.edu.au<mailto:lijun.zhao at adelaide.edu.au> or lijun.zhao at sahmri.com<mailto:lijun.zhao at sahmri.com> [[alternative HTML version deleted]]
William Dunlap
2020-Feb-19 15:22 UTC
[R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.
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, which maps FALSE and NA to FALSE and TRUE to TRUE. I don't think core R contains such a function. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Feb 19, 2020 at 7:08 AM Lijun Zhao <lijun.zhao at adelaide.edu.au> wrote:> 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 give a new index. > I want the output be like y. > > y<-c(1,1,1,1,1,1,2,3,3) > > Thank you so much, > > Lijun Zhao (PhD Candidate) > Nutrition and Metabolism > Level 7 SAHMRI > North Terrace > Adelaide 5005 > Ph : +61 8 8128 4898 > e-mail: lijun.zhao at adelaide.edu.au<mailto:lijun.zhao at adelaide.edu.au> or > lijun.zhao at sahmri.com<mailto:lijun.zhao at sahmri.com> > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Lijun Zhao
2020-Feb-20 03:20 UTC
[R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.
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, 1, 1 ,1, 1 ,1, 2, 3, 3) Therefore, I would like to create y based on the variable a. once variable a has changed, the index will start from 1 again. I wrote a for loop, but it did not give me what I want. Could you please help me again? Thanks in advance, Lijun From: William Dunlap <wdunlap at tibco.com> Sent: Thursday, 20 February 2020 1:53 AM To: Lijun Zhao <lijun.zhao at adelaide.edu.au> Cc: r-help at r-project.org Subject: Re: [R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index. 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<http://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, which maps FALSE and NA to FALSE and TRUE to TRUE. I don't think core R contains such a function. Bill Dunlap TIBCO Software wdunlap tibco.com<http://tibco.com> On Wed, Feb 19, 2020 at 7:08 AM Lijun Zhao <lijun.zhao at adelaide.edu.au<mailto:lijun.zhao at adelaide.edu.au>> wrote: 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 give a new index. I want the output be like y. y<-c(1,1,1,1,1,1,2,3,3) Thank you so much, Lijun Zhao (PhD Candidate) Nutrition and Metabolism Level 7 SAHMRI North Terrace Adelaide 5005 Ph : +61 8 8128 4898 e-mail: lijun.zhao at adelaide.edu.au<mailto:lijun.zhao at adelaide.edu.au><mailto:lijun.zhao at adelaide.edu.au<mailto:lijun.zhao at adelaide.edu.au>> or lijun.zhao at sahmri.com<mailto:lijun.zhao at sahmri.com><mailto:lijun.zhao at sahmri.com<mailto:lijun.zhao at sahmri.com>> [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]]
Apparently Analagous Threads
- How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.
- function(x) !is.na(x) & x "is_true(.)" etc. was: [R] How to index..
- taking daily means from hourly data
- Counting things
- [PATCH 0/3] Allow environment variables to have boolean values.