Hi, Can anyone please help to get "StartDay" and "End-day of a particular month" with time ? For eg:- Input wil be "2012-09-27" i need to get output as given below StartDt <- "2012-09-01 00:00:01" EndDt <- "2012-09-30 23:59:59" - Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Start-and-End-day-of-a-month-tp4644372.html Sent from the R help mailing list archive at Nabble.com.
Start and End day of a month
6 messages · Rantony, R. Michael Weylandt, Joshua Ulrich +2 more
On Thu, Sep 27, 2012 at 1:21 PM, Rantony <antony.akkara at ge.com> wrote:
Hi, Can anyone please help to get "StartDay" and "End-day of a particular month" with time ? For eg:- Input wil be "2012-09-27" i need to get output as given below StartDt <- "2012-09-01 00:00:01" EndDt <- "2012-09-30 23:59:59" - Thanks in advance
Look at xts::lastof(). Cheers, Michael
HI,
For a vector of dates:
Using Michael's suggestion:
library(xts)
library(zoo)
Dt<-c("2012-09-27","2012-09-28","2012-07-24","2012-06-05","2012-12-03")
newDt<-strsplit(format(as.yearmon(Dt),"%Y-%m"),split="-")
StartDt<-do.call(rbind,lapply(lapply(lapply(newDt,`[`,1:2),function(x)
as.numeric(c(x[1],x[2]))),function(x)
format(firstof(x[1],x[2],sec=1),format="%Y-%m-%d %r")))
EndDt<-do.call(rbind,lapply(lapply(lapply(newDt,`[`,1:2),function(x)
as.numeric(c(x[1],x[2]))),function(x)
format(lastof(x[1],x[2]),format="%Y-%m-%d %r")))
dat1<-data.frame(inputDate=Dt,StartDt=StartDt,EndDt=EndDt)
dat1
# ? inputDate ? ? ? ? ? ? ? ?StartDt ? ? ? ? ? ? ? ? ?EndDt
#1 2012-09-27 2012-09-01 12:00:01 AM 2012-09-30 11:59:59 PM
#2 2012-09-28 2012-09-01 12:00:01 AM 2012-09-30 11:59:59 PM
#3 2012-07-24 2012-07-01 12:00:01 AM 2012-07-31 11:59:59 PM
#4 2012-06-05 2012-06-01 12:00:01 AM 2012-06-30 11:59:59 PM
#5 2012-12-03 2012-12-01 12:00:01 AM 2012-12-31 11:59:59 PM
A.K.
----- Original Message -----
From: R. Michael Weylandt <michael.weylandt at gmail.com>
To: Rantony <antony.akkara at ge.com>
Cc: r-help at r-project.org
Sent: Thursday, September 27, 2012 11:33 AM
Subject: Re: [R] Start and End day of a month
On Thu, Sep 27, 2012 at 1:21 PM, Rantony <antony.akkara at ge.com> wrote:
Hi, Can anyone please help to get "StartDay" and "End-day of a particular month" with time ? For eg:- Input wil be "2012-09-27" i need to get output as given below StartDt <- "2012-09-01 00:00:01" EndDt <- "2012-09-30 23:59:59" - Thanks in advance
Look at xts::lastof(). Cheers, Michael ______________________________________________ R-help at r-project.org mailing list 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.
On Thu, Sep 27, 2012 at 3:05 PM, arun <smartpink111 at yahoo.com> wrote:
HI,
For a vector of dates:
Using Michael's suggestion:
library(xts)
library(zoo)
Dt<-c("2012-09-27","2012-09-28","2012-07-24","2012-06-05","2012-12-03")
newDt<-strsplit(format(as.yearmon(Dt),"%Y-%m"),split="-")
StartDt<-do.call(rbind,lapply(lapply(lapply(newDt,`[`,1:2),function(x)
as.numeric(c(x[1],x[2]))),function(x)
format(firstof(x[1],x[2],sec=1),format="%Y-%m-%d %r")))
EndDt<-do.call(rbind,lapply(lapply(lapply(newDt,`[`,1:2),function(x)
as.numeric(c(x[1],x[2]))),function(x)
format(lastof(x[1],x[2]),format="%Y-%m-%d %r")))
dat1<-data.frame(inputDate=Dt,StartDt=StartDt,EndDt=EndDt)
dat1
# inputDate StartDt EndDt
#1 2012-09-27 2012-09-01 12:00:01 AM 2012-09-30 11:59:59 PM
#2 2012-09-28 2012-09-01 12:00:01 AM 2012-09-30 11:59:59 PM
#3 2012-07-24 2012-07-01 12:00:01 AM 2012-07-31 11:59:59 PM
#4 2012-06-05 2012-06-01 12:00:01 AM 2012-06-30 11:59:59 PM
#5 2012-12-03 2012-12-01 12:00:01 AM 2012-12-31 11:59:59 PM
A.K.
That code is very difficult to follow. This is much clearer: dat1 <- data.frame(inputDate=Dt, startDate=do.call(c,lapply(Dt, firstof, 0, 0, 1)), endDate=do.call(c,lapply(Dt, lastof, 23, 59, 59))) -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com
----- Original Message ----- From: R. Michael Weylandt <michael.weylandt at gmail.com> To: Rantony <antony.akkara at ge.com> Cc: r-help at r-project.org Sent: Thursday, September 27, 2012 11:33 AM Subject: Re: [R] Start and End day of a month On Thu, Sep 27, 2012 at 1:21 PM, Rantony <antony.akkara at ge.com> wrote:
Hi, Can anyone please help to get "StartDay" and "End-day of a particular month" with time ? For eg:- Input wil be "2012-09-27" i need to get output as given below StartDt <- "2012-09-01 00:00:01" EndDt <- "2012-09-30 23:59:59" - Thanks in advance
Look at xts::lastof(). Cheers, Michael
______________________________________________ R-help at r-project.org mailing list 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 at r-project.org mailing list 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.
HI Joshua, Thanks for providing much easier solution. But, I am getting the output from dat1 as: #? inputDate?????????? startDate???????????? endDate #1 2012-09-27 2012-09-27 00:00:01 2012-09-27 23:59:59 #2 2012-09-28 2012-09-28 00:00:01 2012-09-28 23:59:59 #3 2012-07-24 2012-07-24 00:00:01 2012-07-24 23:59:59 #4 2012-06-05 2012-06-05 00:00:01 2012-06-05 23:59:59 #5 2012-12-03 2012-12-03 00:00:01 2012-12-03 23:59:59 OP's request: "Can anyone please help to get "StartDay" and "End-day of a particular date" with time ? For eg:- Input wil be "2012-09-27" i need to get output as given below StartDt <- "2012-09-01 12:00:01 AM" EndDt <- "2012-09-30 11:59:59 PM" " May be there is something you forgot to add in the reply. A.K. ----- Original Message ----- From: Joshua Ulrich <josh.m.ulrich at gmail.com> To: arun <smartpink111 at yahoo.com> Cc: Rantony <antony.akkara at ge.com>; R help <r-help at r-project.org> Sent: Thursday, September 27, 2012 11:26 PM Subject: Re: [R] Start and End day of a month
On Thu, Sep 27, 2012 at 3:05 PM, arun <smartpink111 at yahoo.com> wrote:
HI,
For a vector of dates:
Using Michael's suggestion:
library(xts)
library(zoo)
Dt<-c("2012-09-27","2012-09-28","2012-07-24","2012-06-05","2012-12-03")
newDt<-strsplit(format(as.yearmon(Dt),"%Y-%m"),split="-")
StartDt<-do.call(rbind,lapply(lapply(lapply(newDt,`[`,1:2),function(x)
? as.numeric(c(x[1],x[2]))),function(x)
format(firstof(x[1],x[2],sec=1),format="%Y-%m-%d %r")))
EndDt<-do.call(rbind,lapply(lapply(lapply(newDt,`[`,1:2),function(x)
? as.numeric(c(x[1],x[2]))),function(x)
format(lastof(x[1],x[2]),format="%Y-%m-%d %r")))
dat1<-data.frame(inputDate=Dt,StartDt=StartDt,EndDt=EndDt)
dat1
#? inputDate? ? ? ? ? ? ? ? StartDt? ? ? ? ? ? ? ? ? EndDt
#1 2012-09-27 2012-09-01 12:00:01 AM 2012-09-30 11:59:59 PM
#2 2012-09-28 2012-09-01 12:00:01 AM 2012-09-30 11:59:59 PM
#3 2012-07-24 2012-07-01 12:00:01 AM 2012-07-31 11:59:59 PM
#4 2012-06-05 2012-06-01 12:00:01 AM 2012-06-30 11:59:59 PM
#5 2012-12-03 2012-12-01 12:00:01 AM 2012-12-31 11:59:59 PM
A.K.
That code is very difficult to follow.? This is much clearer: dat1 <- data.frame(inputDate=Dt, ? startDate=do.call(c,lapply(Dt, firstof, 0, 0, 1)), ? endDate=do.call(c,lapply(Dt, lastof, 23, 59, 59))) -- Joshua Ulrich? |? about.me/joshuaulrich FOSS Trading? |? www.fosstrading.com
----- Original Message ----- From: R. Michael Weylandt <michael.weylandt at gmail.com> To: Rantony <antony.akkara at ge.com> Cc: r-help at r-project.org Sent: Thursday, September 27, 2012 11:33 AM Subject: Re: [R] Start and End day of a month On Thu, Sep 27, 2012 at 1:21 PM, Rantony <antony.akkara at ge.com> wrote:
Hi, Can anyone please help to get "StartDay" and "End-day of a particular month" with time ? For eg:- Input wil be "2012-09-27" i need to get output as given below StartDt <- "2012-09-01 00:00:01" EndDt <- "2012-09-30 23:59:59" - Thanks in advance
Look at xts::lastof(). Cheers, Michael
______________________________________________ R-help at r-project.org mailing list 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 at r-project.org mailing list 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.
On Thu, Sep 27, 2012 at 8:21 AM, Rantony <antony.akkara at ge.com> wrote:
Hi, Can anyone please help to get "StartDay" and "End-day of a particular month" with time ? For eg:- Input wil be "2012-09-27" i need to get output as given below StartDt <- "2012-09-01 00:00:01" EndDt <- "2012-09-30 23:59:59"
This converts the input to a "yearmon" class object which represents the date as just a year and month. We then convert it back to "Date" class using the frac argument to indicate whether the first or last of the month is to be used. Finally we paste the time to it which also converts it to character. library(zoo) d <- "2012-09-27" ym <- as.yearmon(d) StartDt <- paste(as.Date(ym, frac = 0), "00:00:01") EndDt <- paste(as.Date(ym, frac = 1), "23:59:59")
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com