given a mid-month date, get the month-end date

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

given a mid-month date, get the month-end date

t c-4
  I have a vector of dates.
   
  I wish to find the month end date for each.
   
  Any suggestions?
   
  e.g.
   
  For 12/15/05, I want 12/31/05,
   
  For 10/15/1995, I want 10/31/1995, etc


__________________________________________________



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: given a mid-month date, get the month-end date

jholtman
Here is one way using POSIX: (you can create a function to do this)

> x <- as.POSIXlt('2005-12-16')  # a date
> x
[1] "2005-12-16"
> dput(x)  #structure of the date
structure(list(sec = 0, min = 0, hour = 0, mday = 16, mon = 11,
    year = 105, wday = 5, yday = 349, isdst = 0), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXt", "POSIXlt"))
> x$mday <- 1  # reset to first of the month
> seq(x, by='month', length=2)[2]  # select 2nd number in the sequence
[1] "2006-01-01 EST"
>



On 12/19/05, t c <[hidden email]> wrote:

>
> I have a vector of dates.
>
> I wish to find the month end date for each.
>
> Any suggestions?
>
> e.g.
>
> For 12/15/05, I want 12/31/05,
>
> For 10/15/1995, I want 10/31/1995, etc
>
>
> __________________________________________________
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: given a mid-month date, get the month-end date

jholtman
Forgot you were asking for the end date, so just subtract a day:

> seq(x, by='month', length=2)[2] - 24*3600
[1] "2005-12-31 EST"



On 12/19/05, jim holtman <[hidden email]> wrote:

>
> Here is one way using POSIX: (you can create a function to do this)
>
> > x <- as.POSIXlt('2005-12-16')  # a date
> > x
> [1] "2005-12-16"
> > dput(x)  #structure of the date
> structure(list(sec = 0, min = 0, hour = 0, mday = 16, mon = 11,
>     year = 105, wday = 5, yday = 349, isdst = 0), .Names = c("sec",
> "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
> ), class = c("POSIXt", "POSIXlt"))
> > x$mday <- 1  # reset to first of the month
> > seq(x, by='month', length=2)[2]  # select 2nd number in the sequence
> [1] "2006-01-01 EST"
> >
>
>
>
>  On 12/19/05, t c <[hidden email]> wrote:
> >
> > I have a vector of dates.
> >
> > I wish to find the month end date for each.
> >
> > Any suggestions?
> >
> > e.g.
> >
> > For 12/15/05, I want 12/31/05,
> >
> > For 10/15/1995, I want 10/31/1995, etc
> >
> >
> > __________________________________________________
> >
> >
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 247 0281
>
> What the problem you are trying to solve?




--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: given a mid-month date, get the month-end date

Gabor Grothendieck
In reply to this post by t c-4
The zoo package has a yearmon class with as methods which can be
used:

library(zoo)
dd <- Sys.Date()  # test data

as.Date(as.yearmon(dd), frac = 1)

as.yearmon converts the "Date" class date to a year and month of
class "yearmon" dropping the day and representing it internally in
a way consistent with "ts" class.

as.Date above then converts it back to "Date" class.
Since yearmon dates have no day (they are just a year and
a month) the frac argument is used to indicate what fraction
of the month to use as the day of the month so frac = 0 (the
default) would give the beginning of the month) and frac = 1
gives the end.

On 12/19/05, t c <[hidden email]> wrote:

>  I have a vector of dates.
>
>  I wish to find the month end date for each.
>
>  Any suggestions?
>
>  e.g.
>
>  For 12/15/05, I want 12/31/05,
>
>  For 10/15/1995, I want 10/31/1995, etc
>
>
> __________________________________________________
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: given a mid-month date, get the month-end date

Whit Armstrong
In reply to this post by t c-4
Or add a month, then subtract a day:

Ndays <- function(posix.ct.dates,days) {
    # one day = 60*60*24 = 86400 seconds
    ans <- as.POSIXct(posix.ct.dates) + 86400*days

    # we only have a problem if the date went from
    # DST to ST or from ST to DST
    ans + (as.POSIXlt(posix.ct.dates)$isdst -
as.POSIXlt(ans)$isdst)*3600
}

calendar.eom <- function(x) {
    x.lt <- as.POSIXlt(x)
    mon <- x.lt $mon + 2
    year <- x.lt$year

    # if month was December add a year
    year <- year + as.integer(mon==13)
    mon[mon==13] <- 1

    Ndays(ISOdate(1900+year,mon,1,hour=0,tz=attr(x,"tzone")),-1)
}

x <- seq(as.POSIXct("2001-01-10"),as.POSIXct("2005-12-10"),by="months")
data.frame(before=x,after=calendar.eom(x))




-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Gabor
Grothendieck
Sent: Monday, December 19, 2005 11:59 AM
To: t c
Cc: r-help
Subject: Re: [R] given a mid-month date, get the month-end date

The zoo package has a yearmon class with as methods which can be
used:

library(zoo)
dd <- Sys.Date()  # test data

as.Date(as.yearmon(dd), frac = 1)

as.yearmon converts the "Date" class date to a year and month of class
"yearmon" dropping the day and representing it internally in a way
consistent with "ts" class.

as.Date above then converts it back to "Date" class.
Since yearmon dates have no day (they are just a year and a month) the
frac argument is used to indicate what fraction of the month to use as
the day of the month so frac = 0 (the
default) would give the beginning of the month) and frac = 1 gives the
end.

On 12/19/05, t c <[hidden email]> wrote:

>  I have a vector of dates.
>
>  I wish to find the month end date for each.
>
>  Any suggestions?
>
>  e.g.
>
>  For 12/15/05, I want 12/31/05,
>
>  For 10/15/1995, I want 10/31/1995, etc
>
>
> __________________________________________________
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: given a mid-month date, get the month-end date

Spencer Graves
In reply to this post by Gabor Grothendieck
          If you work much with time data and time series data and you have not
already mastered the "zoo" package, you may be interested in a small
testimonial from one unfamiliar with the names of Achim Zeileis and
Gabor Grothendieck two years ago:  The package itself seems to have many
useful features, filling an important gap in other capabilities, AND
vignette("zoo") makes it relatively easy to learn.

          Those unfamiliar with vignettes in R may wish to know that the command

        > (zoo.vignette <- vignette("zoo"))

will open an Adobe Acrobat file containing a narrative overview of the
package's capabilities.  The R commands used in the file can be easily
obtained in a script file after this command via

          > edit(zoo.vignette)

in RGui;  this should open the R comands in an editor script window.
This may not have the desired effect in XEmacs, which I use.  Instead, I
use the following:

          > Stangle(zoo.vignette$file)

This writes the R commands to file zoo.R in the working directory.  From
there, I then use menu File -> Open -> “zoo.R”.

          hope this helps.
          Spencer Graves
p.s.  In case it's not completely obvious from the above, I wish to here
extend my thanks and complements to Achim Zeileis and Gabor Grothendieck
for a great package with good documentation.

Gabor Grothendieck wrote:

> The zoo package has a yearmon class with as methods which can be
> used:
>
> library(zoo)
> dd <- Sys.Date()  # test data
>
> as.Date(as.yearmon(dd), frac = 1)
>
> as.yearmon converts the "Date" class date to a year and month of
> class "yearmon" dropping the day and representing it internally in
> a way consistent with "ts" class.
>
> as.Date above then converts it back to "Date" class.
> Since yearmon dates have no day (they are just a year and
> a month) the frac argument is used to indicate what fraction
> of the month to use as the day of the month so frac = 0 (the
> default) would give the beginning of the month) and frac = 1
> gives the end.
>
> On 12/19/05, t c <[hidden email]> wrote:
>
>> I have a vector of dates.
>>
>> I wish to find the month end date for each.
>>
>> Any suggestions?
>>
>> e.g.
>>
>> For 12/15/05, I want 12/31/05,
>>
>> For 10/15/1995, I want 10/31/1995, etc
>>
>>
>>__________________________________________________
>>
>>
>>
>>       [[alternative HTML version deleted]]
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

--
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

[hidden email]
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html