Re: [R] supply chain, operations, and sales optimization in R

2020-02-19 Thread Bert Gunter
Ummm... rather vague, and I certainly have no clue. But if you haven't
already done so, have a look here:
https://cran.r-project.org/web/views/

And of course, you should always try googling, e.g. on "supply chain
optimization using R" , etc.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Feb 19, 2020 at 7:14 PM Jeff Reichman 
wrote:

> R-Help Forum
>
>
>
> Anyone ever perform supply chain optimization, operations optimization or
> sales optimization in R? If so what packages should I look to?
>
>
>
>
>
> Sincerely
>
>
>
> Jeff Reichman
>
> (314) 457-1966
>
>
>
>
> [[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] supply chain, operations, and sales optimization in R

2020-02-19 Thread Jeff Reichman
R-Help Forum

 

Anyone ever perform supply chain optimization, operations optimization or
sales optimization in R? If so what packages should I look to?

 

 

Sincerely

 

Jeff Reichman

(314) 457-1966

 


[[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] Annotate question

2020-02-19 Thread Rui Barradas

Hello,

If groups are factors, pass the level you want to annotate.
This works, note the 'x' value:

ggplot(iris, aes(Species, Petal.Length)) +
  geom_boxplot() +
  annotate(geom = "text", x = "versicolor", y = 6, label = "16 u")


Hope this helps,

Rui Barradas


Às 20:26 de 19/02/20, Thomas Subia escreveu:

Colleagues,

To add an annotation using ggplot, I've used annotate("text",x=17,y=2130,label="16 
u").

However, this does not work when trying to annotate box plots by groups since 
groups are factors.

Any advice would be appreciated.

Thomas Subia
ASQ CQE

IMG Companies
225 Mountain Vista Parkway
Livermore, CA 94551
T. (925) 273-1106
F. (925) 273-
E. tsu...@imgprecision.com


Precision Manufacturing for Emerging Technologies
imgprecision.com

The contents of this message, together with any attachments, are intended only 
for the use of the individual or entity to which they are addressed and may 
contain information that is legally privileged, confidential and exempt from 
disclosure. If you are not the intended recipient, you are hereby notified that 
any dissemination, distribution, or copying of this message, or any attachment, 
is strictly prohibited. If you have received this message in error, please 
notify the original sender or IMG Companies, LLC at Tel: 925-273-1100 
immediately by telephone or by return E-mail and delete this message, along 
with any attachments, from your computer. Thank you.

__
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] Annotate question

2020-02-19 Thread Stefan Schreiber
Since factor levels (groups) are coded by integers, you can use 1, 2, 3
etc. as your x values. If you want to annotate in between you can simply
pick values in between 1, 2, 3, etc.


On Wed, Feb 19, 2020, 13:26 Thomas Subia,  wrote:

> Colleagues,
>
> To add an annotation using ggplot, I've used
> annotate("text",x=17,y=2130,label="16 u").
>
> However, this does not work when trying to annotate box plots by groups
> since groups are factors.
>
> Any advice would be appreciated.
>
> Thomas Subia
> ASQ CQE
>
> IMG Companies
> 225 Mountain Vista Parkway
> Livermore, CA 94551
> T. (925) 273-1106
> F. (925) 273-
> E. tsu...@imgprecision.com
>
>
> Precision Manufacturing for Emerging Technologies
> imgprecision.com
>
> The contents of this message, together with any attachments, are intended
> only for the use of the individual or entity to which they are addressed
> and may contain information that is legally privileged, confidential and
> exempt from disclosure. If you are not the intended recipient, you are
> hereby notified that any dissemination, distribution, or copying of this
> message, or any attachment, is strictly prohibited. If you have received
> this message in error, please notify the original sender or IMG Companies,
> LLC at Tel: 925-273-1100 immediately by telephone or by return E-mail and
> delete this message, along with any attachments, from your computer. Thank
> you.
>
> __
> 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] Annotate question

2020-02-19 Thread Thomas Subia
Colleagues,

To add an annotation using ggplot, I've used 
annotate("text",x=17,y=2130,label="16 u").

However, this does not work when trying to annotate box plots by groups since 
groups are factors.

Any advice would be appreciated.

Thomas Subia 
ASQ CQE

IMG Companies 
225 Mountain Vista Parkway
Livermore, CA 94551
T. (925) 273-1106
F. (925) 273-
E. tsu...@imgprecision.com


Precision Manufacturing for Emerging Technologies
imgprecision.com 

The contents of this message, together with any attachments, are intended only 
for the use of the individual or entity to which they are addressed and may 
contain information that is legally privileged, confidential and exempt from 
disclosure. If you are not the intended recipient, you are hereby notified that 
any dissemination, distribution, or copying of this message, or any attachment, 
is strictly prohibited. If you have received this message in error, please 
notify the original sender or IMG Companies, LLC at Tel: 925-273-1100 
immediately by telephone or by return E-mail and delete this message, along 
with any attachments, from your computer. Thank you.

__
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] Converting irregular time series data into ts object

2020-02-19 Thread Gabor Grothendieck
Sorry there were some errors in my email. Use this instead.

Assuming that they both cover the same period of time then
if you are willing to throw away some points then
consider using only these 256 elements from the 305 series

  round(seq(1, 305, length = 256))
  ##  [1]   1   2   3   5

That is use the 1st 2nd, 3rd, 5th, etc. point in each year from the
305 series.  This aligns them by throwing away 305-256=49
points per year in the 305 series so that both series can be
set up with a frequency of 256 points per year.

On Wed, Feb 19, 2020 at 10:37 AM Gabor Grothendieck
 wrote:
>
> Assuming that they both cover the same period of time then
> if you are willing to throw away some points then
> consider using only these 256 elements from the 305 series
>
>   round(seq(1, 305, length = 50))
>   ## [1]   1   7  13  20  26  32  38  44 ...etc...
>
> That is use the 1st ,7th, 13th, etc. point in each year from the
> 305 series.  This aligns them by throwing away 305-256=49
> points per year in the 305 series so that both series can be
> set up with a frequency of 256 points per year.
>
>
>
> On Wed, Feb 19, 2020 at 8:36 AM Upananda Pani  wrote:
> >
> > Dear All,
> >
> > I want to convert irregular time series daily data in to ts objects. For
> > some years I have 305 days data and some years I have 256 days.
> >
> > I need your suggestion regarding the same.
> >
> > I have read tutorial on the same but not able to find solutions.
> >
> > With regards,
> > Upananda
> >
> > [[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.
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at 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] Converting irregular time series data into ts object

2020-02-19 Thread Gabor Grothendieck
Assuming that they both cover the same period of time then
if you are willing to throw away some points then
consider using only these 256 elements from the 305 series

  round(seq(1, 305, length = 50))
  ## [1]   1   7  13  20  26  32  38  44 ...etc...

That is use the 1st ,7th, 13th, etc. point in each year from the
305 series.  This aligns them by throwing away 305-256=49
points per year in the 305 series so that both series can be
set up with a frequency of 256 points per year.



On Wed, Feb 19, 2020 at 8:36 AM Upananda Pani  wrote:
>
> Dear All,
>
> I want to convert irregular time series daily data in to ts objects. For
> some years I have 305 days data and some years I have 256 days.
>
> I need your suggestion regarding the same.
>
> I have read tutorial on the same but not able to find solutions.
>
> With regards,
> Upananda
>
> [[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.



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at 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] Package httr::GET() question

2020-02-19 Thread Roy Mendelssohn - NOAA Federal via R-help
Thanks.  Yes.  I did that,  it also has a verbose mode so that I could see what 
it was doing.  What I needed was not just escaping but strict escaping.  My 
memory forma number of years back was that I had issues with urlencode from 
base not being strict.  And of course you don't what to encode twice.

Thanks,

-Roy


> On Feb 19, 2020, at 7:08 AM, Ben Tupper  wrote:
> 
> Hi,
> 
> Perhaps you could test it out by using httr::GET() with and without
> escaping using xml2::url_escape()?
> 
> https://www.rdocumentation.org/packages/xml2/versions/1.2.2/topics/url_escape
> 
> Cheers,
> Ben
> 
> On Tue, Feb 18, 2020 at 1:29 PM Roy Mendelssohn - NOAA Federal via
> R-help  wrote:
>> 
>> Hi All:
>> 
>> I hav been trying to go through the code for httr::GET() but it is somewhat 
>> beyond what I know.  What I am trying to find out is if all urls are 
>> automatically percent encoded,  or whether the user needs to do that.
>> 
>> Thanks,
>> 
>> -Roy
>> 
>> **
>> "The contents of this message do not reflect any position of the U.S. 
>> Government or NOAA."
>> **
>> Roy Mendelssohn
>> Supervisory Operations Research Analyst
>> NOAA/NMFS
>> Environmental Research Division
>> Southwest Fisheries Science Center
>> ***Note new street address***
>> 110 McAllister Way
>> Santa Cruz, CA 95060
>> Phone: (831)-420-3666
>> Fax: (831) 420-3980
>> e-mail: roy.mendelss...@noaa.gov www: https://www.pfeg.noaa.gov/
>> 
>> "Old age and treachery will overcome youth and skill."
>> "From those who have been given much, much will be expected"
>> "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
>> 
>> __
>> 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.
> 
> 
> 
> -- 
> Ben Tupper
> Bigelow Laboratory for Ocean Science
> West Boothbay Harbor, Maine
> http://www.bigelow.org/
> https://eco.bigelow.org

**
"The contents of this message do not reflect any position of the U.S. 
Government or NOAA."
**
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new street address***
110 McAllister Way
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: roy.mendelss...@noaa.gov www: https://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

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


Re: [R] Package httr::GET() question

2020-02-19 Thread Ben Tupper
Hi,

Perhaps you could test it out by using httr::GET() with and without
escaping using xml2::url_escape()?

https://www.rdocumentation.org/packages/xml2/versions/1.2.2/topics/url_escape

Cheers,
Ben

On Tue, Feb 18, 2020 at 1:29 PM Roy Mendelssohn - NOAA Federal via
R-help  wrote:
>
> Hi All:
>
>  I hav been trying to go through the code for httr::GET() but it is somewhat 
> beyond what I know.  What I am trying to find out is if all urls are 
> automatically percent encoded,  or whether the user needs to do that.
>
> Thanks,
>
> -Roy
>
> **
> "The contents of this message do not reflect any position of the U.S. 
> Government or NOAA."
> **
> Roy Mendelssohn
> Supervisory Operations Research Analyst
> NOAA/NMFS
> Environmental Research Division
> Southwest Fisheries Science Center
> ***Note new street address***
> 110 McAllister Way
> Santa Cruz, CA 95060
> Phone: (831)-420-3666
> Fax: (831) 420-3980
> e-mail: roy.mendelss...@noaa.gov www: https://www.pfeg.noaa.gov/
>
> "Old age and treachery will overcome youth and skill."
> "From those who have been given much, much will be expected"
> "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
>
> __
> 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.



-- 
Ben Tupper
Bigelow Laboratory for Ocean Science
West Boothbay Harbor, Maine
http://www.bigelow.org/
https://eco.bigelow.org

__
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] Converting irregular time series data into ts object

2020-02-19 Thread Jeff Newmiller
You should read about statistical imputation and decide what approach is 
appropriate for your data. This mailing list is for questions about R, not 
about statistics. Once you know what algorithm you need to apply, look up R 
functions that implement that algorithm using Google or the CRAN Task Views.

If you find yourself with some example code that doesn't work the way you think 
it should, that would be an appropriate time to first read the Posting Guide 
and then come back and post a question using plain text email setting rather 
than html format (so we don't receive a scrambled version of your broken 
example code).

On February 19, 2020 5:35:29 AM PST, Upananda Pani  
wrote:
>Dear All,
>
>I want to convert irregular time series daily data in to ts objects.
>For
>some years I have 305 days data and some years I have 256 days.
>
>I need your suggestion regarding the same.
>
>I have read tutorial on the same but not able to find solutions.
>
>With regards,
>Upananda
>
>   [[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.

-- 
Sent from my phone. Please excuse my brevity.

__
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] Converting irregular time series data into ts object

2020-02-19 Thread Upananda Pani
Dear All,

I want to convert irregular time series daily data in to ts objects. For
some years I have 305 days data and some years I have 256 days.

I need your suggestion regarding the same.

I have read tutorial on the same but not able to find solutions.

With regards,
Upananda

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