Re: [R] Getting the last day of the month.

2007-05-10 Thread Eric

For 2007:

seq(as.Date('2007-02-01'), length = 12, by = "mon") - 1

Current month:
seq( as.Date( format( Sys.Date(), "%Y-%m-01")), length = 2, by = 
"mon")[2] - 1

Eric



Patnaik, Tirthankar wrote:
> Hi,
>   Given a date, how do I get the last date of that month? I have
> data in the form MM, that I've read as a date using
>
>   
>> x$Date <-
>> 
> as.Date(ISOdate(substr(x$YearEnd,1,4),substr(x$YearEnd,5,6),1))
>
> But this gives the first day of the month. To get the last day of the
> month, I tried 
>
>   
>> as.Date(as.yearmon(x$Date,frac=0))
>> 
>
> But I don't get the last day of the month here. (Tried frac=1 too.)
>
> I then add a month to the date, substract one day from the resultant
> date. But this wouldn't work for December.
>
>   
>> x$YearEnd
>> 
>  [1] 200203 200303 200403 200503 200603 200603 200312 200503 200603
> 200203 200303
> [12] 200403 200503 200512 200612 200203 200303 200403 200503 200603
>   
>> x$Date <- as.Date(ISOdate(substr(x$YearEnd,1,4),
>> 
> + as.integer(substr(x$YearEnd,5,6))+1,
> + 1))-1
>   
>> x$Date
>> 
>  [1] "2002-03-31" "2003-03-31" "2004-03-31" "2005-03-31" "2006-03-31"
> "2006-03-31"
>  [7] NA   "2005-03-31" "2006-03-31" "2002-03-31" "2003-03-31"
> "2004-03-31"
> [13] "2005-03-31" NA   NA   "2002-03-31" "2003-03-31"
> "2004-03-31"
> [19] "2005-03-31" "2006-03-31"
>
> So I add a year, and set the month to 1 in a quick function.
>
>   
>> GetEOM <- function(mm=200406){
>> 
>   year <- as.integer(substr(mm,1,4))
>   month <- as.integer(substr(mm,5,6))
>   if (month==12){
>   date <- as.Date(ISOdate(year+1,1,1))-1
>   }else{
>   date <- as.Date(ISOdate(year,month+1,1))-1
>   }
>   print(date)
> }
>
> x$Date <- as.vector(sapply(x$YearEnd,GetEOM))
>
> str(x$Date)
>
>
> Is there a simpler way to do this please?
>
>
> TIA and best,
> -Tir
>
> Tirthankar Patnaik
> India Strategy
> Citigroup Investment Research
> +91-22-6631 9887
>
> __
> R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.


Re: [R] Getting the last day of the month.

2007-05-10 Thread gyadav

http://finzi.psych.upenn.edu/R/library/fCalendar/html/3D-TimeDateSpecDates.html

try this also RSiteSearch("last day of the month") to get more pointers


-  Regards,

  \\\|///
   \\   --   //
(  o   o  )
oOOo-(_)-oOOo
|
| Gaurav Yadav
| Assistant Manager, CCIL, Mumbai (India)
| Mob: +919821286118 Email: [EMAIL PROTECTED]
| Man is made by his belief, as He believes, so He is.
|   --- Bhagavad Gita 
|___Oooo
 oooO(  )
 (  )   )   /
  \   ((_/
\_ )




"Patnaik, Tirthankar " <[EMAIL PROTECTED]> 
Sent by: [EMAIL PROTECTED]
05/10/2007 07:12 PM

To

cc

Subject
[R] Getting the last day of the month.






Hi,
 Given a date, how do I get the last date of that month? I 
have
data in the form MM, that I've read as a date using

> x$Date <-
as.Date(ISOdate(substr(x$YearEnd,1,4),substr(x$YearEnd,5,6),1))

But this gives the first day of the month. To get the last day of the
month, I tried 

> as.Date(as.yearmon(x$Date,frac=0))

But I don't get the last day of the month here. (Tried frac=1 too.)

I then add a month to the date, substract one day from the resultant
date. But this wouldn't work for December.

> x$YearEnd
 [1] 200203 200303 200403 200503 200603 200603 200312 200503 200603
200203 200303
[12] 200403 200503 200512 200612 200203 200303 200403 200503 200603
> 
> x$Date <- as.Date(ISOdate(substr(x$YearEnd,1,4),
+ as.integer(substr(x$YearEnd,5,6))+1,
+ 1))-1
> x$Date
 [1] "2002-03-31" "2003-03-31" "2004-03-31" "2005-03-31" "2006-03-31"
"2006-03-31"
 [7] NA   "2005-03-31" "2006-03-31" "2002-03-31" "2003-03-31"
"2004-03-31"
[13] "2005-03-31" NA   NA   "2002-03-31" "2003-03-31"
"2004-03-31"
[19] "2005-03-31" "2006-03-31"

So I add a year, and set the month to 1 in a quick function.

> GetEOM <- function(mm=200406){
 year <- as.integer(substr(mm,1,4))
 month <- as.integer(substr(mm,5,6))
 if (month==12){
 date <- as.Date(ISOdate(year+1,1,1))-1
 }else{
 date <- 
as.Date(ISOdate(year,month+1,1))-1
 }
 print(date)
}

x$Date <- as.vector(sapply(x$YearEnd,GetEOM))

str(x$Date)


Is there a simpler way to do this please?


TIA and best,
-Tir

Tirthankar Patnaik
India Strategy
Citigroup Investment Research
+91-22-6631 9887

__
R-help@stat.math.ethz.ch 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.




DISCLAIMER AND CONFIDENTIALITY CAUTION:\ \ This message and ...{{dropped}}

__
R-help@stat.math.ethz.ch 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.


Re: [R] Getting the last day of the month.

2007-05-10 Thread Jeffrey J. Hallman
The TimeIndex class in the 'fame' package handles this kind of stuff with ease.
> library(fame)
> ym <- 200212
> z <- lastDayOf(ti(100*ym + 1, tif = "monthly"))
> z
[1] 20021231
class: ti
> tifName(z)
[1] "daily"

a 'ti' object is a TimeIndex, and it has a tif (TimeIndexFrequency) embedded
in it. 
-- 
Jeff

__
R-help@stat.math.ethz.ch 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.


Re: [R] Getting the last day of the month.

2007-05-10 Thread Gabor Grothendieck
Try this:

library(zoo)
x <- "200212"
as.Date(as.yearmon(paste(x, "01", sep = ""), "%Y%m%d"), frac = 1)



On 5/10/07, Patnaik, Tirthankar <[EMAIL PROTECTED]> wrote:
> Hi,
>Given a date, how do I get the last date of that month? I have
> data in the form MM, that I've read as a date using
>
> > x$Date <-
> as.Date(ISOdate(substr(x$YearEnd,1,4),substr(x$YearEnd,5,6),1))
>
> But this gives the first day of the month. To get the last day of the
> month, I tried
>
> > as.Date(as.yearmon(x$Date,frac=0))
>
> But I don't get the last day of the month here. (Tried frac=1 too.)
>
> I then add a month to the date, substract one day from the resultant
> date. But this wouldn't work for December.
>
> > x$YearEnd
>  [1] 200203 200303 200403 200503 200603 200603 200312 200503 200603
> 200203 200303
> [12] 200403 200503 200512 200612 200203 200303 200403 200503 200603
> >
> > x$Date <- as.Date(ISOdate(substr(x$YearEnd,1,4),
> + as.integer(substr(x$YearEnd,5,6))+1,
> + 1))-1
> > x$Date
>  [1] "2002-03-31" "2003-03-31" "2004-03-31" "2005-03-31" "2006-03-31"
> "2006-03-31"
>  [7] NA   "2005-03-31" "2006-03-31" "2002-03-31" "2003-03-31"
> "2004-03-31"
> [13] "2005-03-31" NA   NA   "2002-03-31" "2003-03-31"
> "2004-03-31"
> [19] "2005-03-31" "2006-03-31"
>
> So I add a year, and set the month to 1 in a quick function.
>
> > GetEOM <- function(mm=200406){
>year <- as.integer(substr(mm,1,4))
>month <- as.integer(substr(mm,5,6))
>if (month==12){
>date <- as.Date(ISOdate(year+1,1,1))-1
>}else{
>date <- as.Date(ISOdate(year,month+1,1))-1
>}
>print(date)
> }
>
> x$Date <- as.vector(sapply(x$YearEnd,GetEOM))
>
> str(x$Date)
>
>
> Is there a simpler way to do this please?
>
>
> TIA and best,
> -Tir
>
> Tirthankar Patnaik
> India Strategy
> Citigroup Investment Research
> +91-22-6631 9887
>
> __
> R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] Getting the last day of the month.

2007-05-10 Thread Patnaik, Tirthankar
Hi,
Given a date, how do I get the last date of that month? I have
data in the form MM, that I've read as a date using

> x$Date <-
as.Date(ISOdate(substr(x$YearEnd,1,4),substr(x$YearEnd,5,6),1))

But this gives the first day of the month. To get the last day of the
month, I tried 

> as.Date(as.yearmon(x$Date,frac=0))

But I don't get the last day of the month here. (Tried frac=1 too.)

I then add a month to the date, substract one day from the resultant
date. But this wouldn't work for December.

> x$YearEnd
 [1] 200203 200303 200403 200503 200603 200603 200312 200503 200603
200203 200303
[12] 200403 200503 200512 200612 200203 200303 200403 200503 200603
> 
> x$Date <- as.Date(ISOdate(substr(x$YearEnd,1,4),
+ as.integer(substr(x$YearEnd,5,6))+1,
+ 1))-1
> x$Date
 [1] "2002-03-31" "2003-03-31" "2004-03-31" "2005-03-31" "2006-03-31"
"2006-03-31"
 [7] NA   "2005-03-31" "2006-03-31" "2002-03-31" "2003-03-31"
"2004-03-31"
[13] "2005-03-31" NA   NA   "2002-03-31" "2003-03-31"
"2004-03-31"
[19] "2005-03-31" "2006-03-31"

So I add a year, and set the month to 1 in a quick function.

> GetEOM <- function(mm=200406){
year <- as.integer(substr(mm,1,4))
month <- as.integer(substr(mm,5,6))
if (month==12){
date <- as.Date(ISOdate(year+1,1,1))-1
}else{
date <- as.Date(ISOdate(year,month+1,1))-1
}
print(date)
}

x$Date <- as.vector(sapply(x$YearEnd,GetEOM))

str(x$Date)


Is there a simpler way to do this please?


TIA and best,
-Tir

Tirthankar Patnaik
India Strategy
Citigroup Investment Research
+91-22-6631 9887

__
R-help@stat.math.ethz.ch 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.