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]]
PIKAL Petr
2020-Feb-21 09:16 UTC
[R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.
Hi If you used diff construction you would not have NA values in beginning #get rid of NA values> d[is.na(d)] <- 0#split d according to a> d.s <- split(d, a)# get the result> lapply(d.s, function(x) cumsum(x>15)+1)$`1` [1] 1 1 1 1 1 1 2 3 3 $`2` [1] 1 1 1 1 1 1 2 3 3 #unlist it> res <- lapply(d.s, function(x) cumsum(x>15)+1) > unlist(res)11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29 1 1 1 1 1 1 2 3 3 1 1 1 1 1 1 2 3 3 Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Lijun Zhao > Sent: Thursday, February 20, 2020 4:21 AM > To: William Dunlap <wdunlap at tibco.com> > 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. > > Dear William, > Thank you so much. > > I am quiet new in R. I would like to do this based on another repeatedvariables.> > 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) theoutcome 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 variablea has> changed, the index will start from 1 again. I wrote a for loop, but it didnot 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 mapsFALSE> 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 thefollowing> 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, itwill 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.z> hao 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 sa > hmri.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
peter dalgaard
2020-Feb-21 09:46 UTC
[R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.
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 implementation of is_true, of course.) -pd> On 20 Feb 2020, at 04:20 , Lijun Zhao <lijun.zhao at adelaide.edu.au> wrote: > > > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Martin Maechler
2020-Feb-21 11:11 UTC
[Rd] function(x) !is.na(x) & x "is_true(.)" etc. was: [R] How to index..
Diverted from R-help to R-devel, as I've changed the focus to consider adding new functions to R :>>>>> peter dalgaard >>>>> on Fri, 21 Feb 2020 10:46:16 +0100 writes:> 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 implementation of is_true, of course.) > -pd yes... We've had (hidden) functions in the Matrix package for these, which can still be accelerated (quite a bit for most arguments), in Matrix/R/Auxiliaries.R (see the R-forge development version : https://r-forge.r-project.org/scm/viewvc.php/pkg/Matrix/R/Auxiliaries.R?view=markup&root=matrix ) ## Need to consider NAs ; "== 0" even works for logical & complex: ## Note that "!x" is faster than "x == 0", but does not (yet!) work for complex ## if we did these in C, would gain a factor 2 (or so): is0 <- function(x) !is.na(x) & x == 0 isN0 <- function(x) is.na(x) | x != 0 is1 <- function(x) !is.na(x) & x # also == "isTRUE componentwise" {{Note that here 0 <==> FALSE and non-0 <==> TRUE , and I had preferred the shorter words to the longer ones in the Matrix pkg, not the least as '0' is relevant also for talking/programming about sparse matrices .. }} ... and then there are all* and any* versions of these functions there , for which I have even written fast C based .Call()s ... May this be something worth adding to base for R 4.0.0 (if only just to entice more users making the effort to switch to it in ca. 2 months) ? Martin >> On 20 Feb 2020, at 04:20 , Lijun Zhao <lijun.zhao at adelaide.edu.au> wrote: >> >> >> 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. > -- > Peter Dalgaard, Professor, > Center for Statistics, Copenhagen Business School > Solbjerg Plads 3, 2000 Frederiksberg, Denmark > Phone: (+45)38153501 > Office: A 4.23 > Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com > ______________________________________________ > 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.
William Dunlap
2020-Feb-21 17:25 UTC
[R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.
> 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 <lijun.zhao at adelaide.edu.au> wrote:> 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(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-24 00:35 UTC
[R] How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.
Hi Peter, Thank you so much. Kind regards, Lijun -----Original Message----- From: peter dalgaard <pdalgd at gmail.com> Sent: Friday, 21 February 2020 8:16 PM To: Lijun Zhao <lijun.zhao at adelaide.edu.au> Cc: William Dunlap <wdunlap at tibco.com>; 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. 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 implementation of is_true, of course.) -pd> On 20 Feb 2020, at 04:20 , Lijun Zhao <lijun.zhao at adelaide.edu.au> wrote: > > > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Apparently Analagous Threads
- [R] Why does R replace all row values with NAs
- [R] Why does R replace all row values with NAs
- [R] Why does R replace all row values with NAs
- [R] Why does R replace all row values with NAs
- How to index the occasions in a vector repeatedly under condition 1? if not, it will give a new index.