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 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  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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


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 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 
> *Sent:* Thursday, 20 February 2020 1:53 AM
> *To:* Lijun Zhao 
> *Cc:* r-help@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 
> 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.z...@adelaide.edu.au or
> lijun.z...@sahmri.com
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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]]

__
R-help@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.


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 implementation of is_true, of course.)

-pd

> On 20 Feb 2020, at 04:20 , Lijun Zhao  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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


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, 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 
Sent: Thursday, 20 February 2020 1:53 AM
To: Lijun Zhao 
Cc: r-help@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 
mailto:lijun.z...@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.z...@adelaide.edu.au>
 or 
lijun.z...@sahmri.com>



[[alternative HTML version deleted]]

__
R-help@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]]

__
R-help@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.


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, 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 
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.z...@adelaide.edu.au or
> lijun.z...@sahmri.com
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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]]

__
R-help@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.


[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 
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.z...@adelaide.edu.au or 
lijun.z...@sahmri.com



[[alternative HTML version deleted]]

__
R-help@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.


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 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
Subject: Re: [R] How to index the occasions in a vector repeatedly under
condition 1? if not, it will give a new index.

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 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

That's the one. When you have a package loaded in your session, please start
your scripts with library(), 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,

Rui Barradas


Às 06:56 de 19/02/20, Lijun Zhao escreveu:

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 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@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.



__
R-help@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.

__
R-help@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.


__
R-help@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.


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
> Subject: Re: [R] How to index the occasions in a vector repeatedly under
> condition 1? if not, it will give a new index.
> 
> 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 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
> 
> That's the one. When you have a package loaded in your session, please start
> your scripts with library(), 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,
> 
> Rui Barradas
> 
> 
> Às 06:56 de 19/02/20, Lijun Zhao escreveu:
> > 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 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@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.
> >
> 
> __
> R-help@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.
__
R-help@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.


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 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

That's the one. When you have a package loaded in your session, please 
start your scripts with library(), 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,

Rui Barradas


Às 06:56 de 19/02/20, Lijun Zhao escreveu:

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 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@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.



__
R-help@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.


[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 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@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.