

Dear R users,
I have two dates, ["open", "close"], which can be any dates such "close" is strictly later than "open".
I wanted to write an R code that displays the following: To construct a vector "v" with all 1st January
days located between "open" and "close" dates: v = (dp_1, dp_2, ..., dp_n). Moreover:
a) Regard to the first date "dp_1":
a1) If "open" is on a 1st January day, "dp_1" is on 1st January of the year after "open".
a2) In case difftime between "open" and the following 1st January is lower than 30 days,
"dp_1" will be on 1st January of the year after the year of that 1st January.
b) Regard to the last date "dp_n":
b1) If "close" is on a 1st January day, dp_n is on 1st January of the year before "close"
b2) In case difftime between "close" and the previous 1st January is lower than 30 days,
"dp_n" will be on 1st January of the year before the year of that 1st January.
Example 1: [open = 20070101, close = 20110405]
v = (20080101, 20090101, 20100101, 20110101) # Since open is already on a 1st January
# Since difftime(20110405, 20110101) >= 30 days
Example 2: [open = 20061215, close = 20110119]
v = (20080101, 20090101, 20100101) # Since difftime(20070101, 20061215) < 30 days
# Since difftime(20110119, 20110101) < 30 days
My code is (for example 2):
open < as.Date('20061215')
close < as.Date('20110119')
dp_1 < as.Date(ifelse(
as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 1), 1, 1, sep = ""))  open >= 30,
as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 1), 1, 1, sep = "")),
as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 2), 1, 1, sep = ""))),
origin = "19700101")
dp_n < as.Date(ifelse(
close  as.Date(paste(as.character(as.numeric(format(close, "%Y"))), 1, 1, sep = "")) >= 30,
as.Date(paste(as.character(as.numeric(format(close, "%Y"))), 1, 1, sep = "")),
as.Date(paste(as.character(as.numeric(format(close, "%Y"))  1), 1, 1, sep = ""))),
origin = "19700101")
v < seq(dp_1, dp_n, by = "year")
However, above code is not too large, so I'm almost sure that there might be a better way of doing it.
Is there a better way to get the vector "v"?
Thanks for any help!
Frank S.
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


I would probably try a different strategy.
First, construct a sequence of January 1 dates that is a little bit too
long. For example, start with the January in the same year as "open" and
finish with the January in the same year as "close". You can construct the
vector using seq(), like this:
> seq(as.Date('200711'), as.Date('201111'), by='year')
[1] "20070101" "20080101" "20090101" "20100101" "20110101"
Then test all those dates for whether they fit your rules, and remove
those that don't.
I think this will be easier than constructing fancy ifelse statements. It
should certainly be easier to understand the code, i.e., when you come
back and look at it a few years from now. Or if a colleague wants to look
at it and understand it.
Don

Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L627
Livermore, CA 94550
9254231062
On 9/30/16, 9:38 AM, "Rhelp on behalf of Frank S."
< [hidden email] on behalf of [hidden email]> wrote:
>Dear R users,
>
>I have two dates, ["open", "close"], which can be any dates such "close"
>is strictly later than "open".
>I wanted to write an R code that displays the following: To construct a
>vector "v" with all 1st January
>
>days located between "open" and "close" dates: v = (dp_1, dp_2, ...,
>dp_n). Moreover:
>
>a) Regard to the first date "dp_1":
> a1) If "open" is on a 1st January day, "dp_1" is on 1st January of
>the year after "open".
> a2) In case difftime between "open" and the following 1st January is
>lower than 30 days,
> "dp_1" will be on 1st January of the year after the year of that
>1st January.
>
>b) Regard to the last date "dp_n":
> b1) If "close" is on a 1st January day, dp_n is on 1st January of the
>year before "close"
> b2) In case difftime between "close" and the previous 1st January is
>lower than 30 days,
> "dp_n" will be on 1st January of the year before the year of that
>1st January.
>
>Example 1: [open = 20070101, close = 20110405]
>v = (20080101, 20090101, 20100101, 20110101) # Since open is
>already on a 1st January
>
> # Since difftime(20110405, 20110101) >= 30
>days
>Example 2: [open = 20061215, close = 20110119]
>v = (20080101, 20090101, 20100101) # Since
>difftime(20070101, 20061215) < 30 days
>
> # Since difftime(20110119, 20110101) < 30 days
>
>
>My code is (for example 2):
>
>open < as.Date('20061215')
>close < as.Date('20110119')
>
>dp_1 < as.Date(ifelse(
> as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 1), 1, 1,
>sep = ""))  open >= 30,
> as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 1), 1, 1,
>sep = "")),
> as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 2), 1, 1,
>sep = ""))),
> origin = "19700101")
>
>dp_n < as.Date(ifelse(
> close  as.Date(paste(as.character(as.numeric(format(close,
>"%Y"))), 1, 1, sep = "")) >= 30,
> as.Date(paste(as.character(as.numeric(format(close, "%Y"))), 1, 1,
>sep = "")),
> as.Date(paste(as.character(as.numeric(format(close, "%Y"))  1), 1, 1,
>sep = ""))),
> origin = "19700101")
>
>v < seq(dp_1, dp_n, by = "year")
>
>
>However, above code is not too large, so I'm almost sure that there might
>be a better way of doing it.
>Is there a better way to get the vector "v"?
>
>Thanks for any help!
>
>Frank S.
>
> [[alternative HTML version deleted]]
>
>______________________________________________
> [hidden email] mailing list  To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/rhelp>PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html>and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Don, many thanks for your tip. I will apply it in the future when programming my code.
Best,
Fran K.
________________________________
De: MacQueen, Don < [hidden email]>
Enviat el: divendres, 30 de setembre de 2016 18:56:45
Per a: Frank S.; [hidden email]
Tema: Re: [R] Elegant way to get specific dates within prespecified period
I would probably try a different strategy.
First, construct a sequence of January 1 dates that is a little bit too
long. For example, start with the January in the same year as "open" and
finish with the January in the same year as "close". You can construct the
vector using seq(), like this:
> seq(as.Date('200711'), as.Date('201111'), by='year')
[1] "20070101" "20080101" "20090101" "20100101" "20110101"
Then test all those dates for whether they fit your rules, and remove
those that don't.
I think this will be easier than constructing fancy ifelse statements. It
should certainly be easier to understand the code, i.e., when you come
back and look at it a few years from now. Or if a colleague wants to look
at it and understand it.
Don

Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L627
Livermore, CA 94550
9254231062
On 9/30/16, 9:38 AM, "Rhelp on behalf of Frank S."
< [hidden email] on behalf of [hidden email]> wrote:
>Dear R users,
>
>I have two dates, ["open", "close"], which can be any dates such "close"
>is strictly later than "open".
>I wanted to write an R code that displays the following: To construct a
>vector "v" with all 1st January
>
>days located between "open" and "close" dates: v = (dp_1, dp_2, ...,
>dp_n). Moreover:
>
>a) Regard to the first date "dp_1":
> a1) If "open" is on a 1st January day, "dp_1" is on 1st January of
>the year after "open".
> a2) In case difftime between "open" and the following 1st January is
>lower than 30 days,
> "dp_1" will be on 1st January of the year after the year of that
>1st January.
>
>b) Regard to the last date "dp_n":
> b1) If "close" is on a 1st January day, dp_n is on 1st January of the
>year before "close"
> b2) In case difftime between "close" and the previous 1st January is
>lower than 30 days,
> "dp_n" will be on 1st January of the year before the year of that
>1st January.
>
>Example 1: [open = 20070101, close = 20110405]
>v = (20080101, 20090101, 20100101, 20110101) # Since open is
>already on a 1st January
>
> # Since difftime(20110405, 20110101) >= 30
>days
>Example 2: [open = 20061215, close = 20110119]
>v = (20080101, 20090101, 20100101) # Since
>difftime(20070101, 20061215) < 30 days
>
> # Since difftime(20110119, 20110101) < 30 days
>
>
>My code is (for example 2):
>
>open < as.Date('20061215')
>close < as.Date('20110119')
>
>dp_1 < as.Date(ifelse(
> as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 1), 1, 1,
>sep = ""))  open >= 30,
> as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 1), 1, 1,
>sep = "")),
> as.Date(paste(as.character(as.numeric(format(open, "%Y")) + 2), 1, 1,
>sep = ""))),
> origin = "19700101")
>
>dp_n < as.Date(ifelse(
> close  as.Date(paste(as.character(as.numeric(format(close,
>"%Y"))), 1, 1, sep = "")) >= 30,
> as.Date(paste(as.character(as.numeric(format(close, "%Y"))), 1, 1,
>sep = "")),
> as.Date(paste(as.character(as.numeric(format(close, "%Y"))  1), 1, 1,
>sep = ""))),
> origin = "19700101")
>
>v < seq(dp_1, dp_n, by = "year")
>
>
>However, above code is not too large, so I'm almost sure that there might
>be a better way of doing it.
>Is there a better way to get the vector "v"?
>
[[elided Hotmail spam]]
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list  To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

