Re: [R] Date seq question

2012-01-21 Thread Gabor Grothendieck
On Fri, Jan 20, 2012 at 12:12 PM, cameron raymond...@invesco.com wrote:
 Can anyone please help me with this?
 I have a list of business dates.  What I want is to have last day of last
 month and paste them on next month.

 What i have                        What i want
 5725 2011-09-22
 5726 2011-09-23
 5727 2011-09-26
 5728 2011-09-27
 5729 2011-09-28
 5730 2011-09-29
 5731 2011-09-30
 5742 2011-10-17                 2011-09-30
 5743 2011-10-18                 2011-09-30
 5744 2011-10-19                 2011-09-30
 5745 2011-10-20                 2011-09-30
 5746 2011-10-21                 2011-09-30
 5747 2011-10-24                 2011-09-30
 5748 2011-10-25                 2011-09-30
 *5749 2011-10-26*                 2011-09-30
 5765 2011-11-17                 2011-10-26
 5766 2011-11-18                 2011-10-26
 5767 2011-11-21                 2011-10-26
 5768 2011-11-22                 2011-10-26
 5769 2011-11-23                 2011-10-26
 5770 2011-11-25                 2011-10-26
 5771 2011-11-28                 2011-10-26
 5772 2011-11-29                 2011-10-26
 *5773 2011-11-30*                 2011-10-26
 5780 2011-12-09                 2011-11-30
 5781 2011-12-12                 2011-11-30
 5782 2011-12-13                 2011-11-30
 5783 2011-12-14                 2011-11-30
 5784 2011-12-15                 2011-11-30
 5785 2011-12-16                 2011-11-30
 5786 2011-12-19                 2011-11-30
 5787 2011-12-20                 2011-11-30
 5788 2011-12-21                 2011-11-30
 5789 2011-12-22                 2011-11-30

 date -
 c(9/22/2011,9/23/2011,9/26/2011,9/27/2011,9/28/2011,9/29/2011,9/30/2011,10/17/2011,
 10/18/2011,10/19/2011,10/20/2011,10/21/2011,10/24/2011,10/25/2011,10/26/2011,11/17/2011,11/18/2011,11/21/2011,11/22/2011,11/23/2011,11/25/2011,11/28/2011,11/29/2011,11/30/2011,
 12/9/2011,12/12/2011,12/13/2011,12/14/2011,12/15/2011,12/16/2011,12/19/2011,12/20/2011,12/21/2011,12/22/2011)


Try this.  It only uses plain R.  f inputs a Date class date and
returns the index in dt which contains the last date in the prior
month (or NA if none) assuming that dt is sorted:

dt - as.Date(date, %m/%d/%Y)

ym - format(dt, %Y %m)
f - function(x) tail(c(NA, which(ym  x)), 1)
dt[sapply(ym, f)]

-- 
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
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] Date seq question

2012-01-21 Thread Gabor Grothendieck
On Sat, Jan 21, 2012 at 5:20 PM, Gabor Grothendieck
ggrothendi...@gmail.com wrote:
 On Fri, Jan 20, 2012 at 12:12 PM, cameron raymond...@invesco.com wrote:
 Can anyone please help me with this?
 I have a list of business dates.  What I want is to have last day of last
 month and paste them on next month.

 What i have                        What i want
 5725 2011-09-22
 5726 2011-09-23
 5727 2011-09-26
 5728 2011-09-27
 5729 2011-09-28
 5730 2011-09-29
 5731 2011-09-30
 5742 2011-10-17                 2011-09-30
 5743 2011-10-18                 2011-09-30
 5744 2011-10-19                 2011-09-30
 5745 2011-10-20                 2011-09-30
 5746 2011-10-21                 2011-09-30
 5747 2011-10-24                 2011-09-30
 5748 2011-10-25                 2011-09-30
 *5749 2011-10-26*                 2011-09-30
 5765 2011-11-17                 2011-10-26
 5766 2011-11-18                 2011-10-26
 5767 2011-11-21                 2011-10-26
 5768 2011-11-22                 2011-10-26
 5769 2011-11-23                 2011-10-26
 5770 2011-11-25                 2011-10-26
 5771 2011-11-28                 2011-10-26
 5772 2011-11-29                 2011-10-26
 *5773 2011-11-30*                 2011-10-26
 5780 2011-12-09                 2011-11-30
 5781 2011-12-12                 2011-11-30
 5782 2011-12-13                 2011-11-30
 5783 2011-12-14                 2011-11-30
 5784 2011-12-15                 2011-11-30
 5785 2011-12-16                 2011-11-30
 5786 2011-12-19                 2011-11-30
 5787 2011-12-20                 2011-11-30
 5788 2011-12-21                 2011-11-30
 5789 2011-12-22                 2011-11-30

 date -
 c(9/22/2011,9/23/2011,9/26/2011,9/27/2011,9/28/2011,9/29/2011,9/30/2011,10/17/2011,
 10/18/2011,10/19/2011,10/20/2011,10/21/2011,10/24/2011,10/25/2011,10/26/2011,11/17/2011,11/18/2011,11/21/2011,11/22/2011,11/23/2011,11/25/2011,11/28/2011,11/29/2011,11/30/2011,
 12/9/2011,12/12/2011,12/13/2011,12/14/2011,12/15/2011,12/16/2011,12/19/2011,12/20/2011,12/21/2011,12/22/2011)


 Try this.  It only uses plain R.  f inputs a Date class date and
 returns the index in dt which contains the last date in the prior
 month (or NA if none) assuming that dt is sorted:

 dt - as.Date(date, %m/%d/%Y)

 ym - format(dt, %Y %m)
 f - function(x) tail(c(NA, which(ym  x)), 1)
 dt[sapply(ym, f)]

Just one correction.  f takes a ym-style string, not a Date.

-- 
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
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] Date seq question

2012-01-21 Thread jim holtman
Here is another way:

 date -
+ 
c(9/22/2011,9/23/2011,9/26/2011,9/27/2011,9/28/2011,9/29/2011,9/30/2011,10/17/2011,
+ 
10/18/2011,10/19/2011,10/20/2011,10/21/2011,10/24/2011,10/25/2011,10/26/2011,11/17/2011,11/18/2011,11/21/2011,11/22/2011,11/23/2011,11/25/2011,11/28/2011,11/29/2011,11/30/2011,
+ 
12/9/2011,12/12/2011,12/13/2011,12/14/2011,12/15/2011,12/16/2011,12/19/2011,12/20/2011,12/21/2011,12/22/2011)
 x.input - as.POSIXlt(date, format = '%m/%d/%Y', tz = GMT)
 x - x.input  # make a copy
 # force to the first day of the month
 x$mday - 1L
 # now get last day of previous month
 x - x - 24 * 3600  # subtract 24 hours (in seconds)
 data.frame(today = x.input, last = x)
today   last
1  2011-09-22 2011-08-31
2  2011-09-23 2011-08-31
3  2011-09-26 2011-08-31
4  2011-09-27 2011-08-31
5  2011-09-28 2011-08-31
6  2011-09-29 2011-08-31
7  2011-09-30 2011-08-31
8  2011-10-17 2011-09-30
9  2011-10-18 2011-09-30
10 2011-10-19 2011-09-30
11 2011-10-20 2011-09-30
12 2011-10-21 2011-09-30
13 2011-10-24 2011-09-30
14 2011-10-25 2011-09-30


On Sat, Jan 21, 2012 at 5:21 PM, Gabor Grothendieck
ggrothendi...@gmail.com wrote:
 On Sat, Jan 21, 2012 at 5:20 PM, Gabor Grothendieck
 ggrothendi...@gmail.com wrote:
 On Fri, Jan 20, 2012 at 12:12 PM, cameron raymond...@invesco.com wrote:
 Can anyone please help me with this?
 I have a list of business dates.  What I want is to have last day of last
 month and paste them on next month.

 What i have                        What i want
 5725 2011-09-22
 5726 2011-09-23
 5727 2011-09-26
 5728 2011-09-27
 5729 2011-09-28
 5730 2011-09-29
 5731 2011-09-30
 5742 2011-10-17                 2011-09-30
 5743 2011-10-18                 2011-09-30
 5744 2011-10-19                 2011-09-30
 5745 2011-10-20                 2011-09-30
 5746 2011-10-21                 2011-09-30
 5747 2011-10-24                 2011-09-30
 5748 2011-10-25                 2011-09-30
 *5749 2011-10-26*                 2011-09-30
 5765 2011-11-17                 2011-10-26
 5766 2011-11-18                 2011-10-26
 5767 2011-11-21                 2011-10-26
 5768 2011-11-22                 2011-10-26
 5769 2011-11-23                 2011-10-26
 5770 2011-11-25                 2011-10-26
 5771 2011-11-28                 2011-10-26
 5772 2011-11-29                 2011-10-26
 *5773 2011-11-30*                 2011-10-26
 5780 2011-12-09                 2011-11-30
 5781 2011-12-12                 2011-11-30
 5782 2011-12-13                 2011-11-30
 5783 2011-12-14                 2011-11-30
 5784 2011-12-15                 2011-11-30
 5785 2011-12-16                 2011-11-30
 5786 2011-12-19                 2011-11-30
 5787 2011-12-20                 2011-11-30
 5788 2011-12-21                 2011-11-30
 5789 2011-12-22                 2011-11-30

 date -
 c(9/22/2011,9/23/2011,9/26/2011,9/27/2011,9/28/2011,9/29/2011,9/30/2011,10/17/2011,
 10/18/2011,10/19/2011,10/20/2011,10/21/2011,10/24/2011,10/25/2011,10/26/2011,11/17/2011,11/18/2011,11/21/2011,11/22/2011,11/23/2011,11/25/2011,11/28/2011,11/29/2011,11/30/2011,
 12/9/2011,12/12/2011,12/13/2011,12/14/2011,12/15/2011,12/16/2011,12/19/2011,12/20/2011,12/21/2011,12/22/2011)


 Try this.  It only uses plain R.  f inputs a Date class date and
 returns the index in dt which contains the last date in the prior
 month (or NA if none) assuming that dt is sorted:

 dt - as.Date(date, %m/%d/%Y)

 ym - format(dt, %Y %m)
 f - function(x) tail(c(NA, which(ym  x)), 1)
 dt[sapply(ym, f)]

 Just one correction.  f takes a ym-style string, not a Date.

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



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

__
R-help@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] Date seq question

2012-01-20 Thread cameron
Can anyone please help me with this?
I have a list of business dates.  What I want is to have last day of last
month and paste them on next month.

What i haveWhat i want
5725 2011-09-22
5726 2011-09-23
5727 2011-09-26
5728 2011-09-27
5729 2011-09-28
5730 2011-09-29
5731 2011-09-30
5742 2011-10-17 2011-09-30
5743 2011-10-18 2011-09-30
5744 2011-10-19 2011-09-30
5745 2011-10-20 2011-09-30
5746 2011-10-21 2011-09-30
5747 2011-10-24 2011-09-30
5748 2011-10-25 2011-09-30
*5749 2011-10-26* 2011-09-30
5765 2011-11-17 2011-10-26
5766 2011-11-18 2011-10-26
5767 2011-11-21 2011-10-26
5768 2011-11-22 2011-10-26
5769 2011-11-23 2011-10-26
5770 2011-11-25 2011-10-26
5771 2011-11-28 2011-10-26
5772 2011-11-29 2011-10-26
*5773 2011-11-30* 2011-10-26
5780 2011-12-09 2011-11-30
5781 2011-12-12 2011-11-30
5782 2011-12-13 2011-11-30
5783 2011-12-14 2011-11-30
5784 2011-12-15 2011-11-30
5785 2011-12-16 2011-11-30
5786 2011-12-19 2011-11-30
5787 2011-12-20 2011-11-30
5788 2011-12-21 2011-11-30
5789 2011-12-22 2011-11-30

date -
c(9/22/2011,9/23/2011,9/26/2011,9/27/2011,9/28/2011,9/29/2011,9/30/2011,10/17/2011,
 
10/18/2011,10/19/2011,10/20/2011,10/21/2011,10/24/2011,10/25/2011,10/26/2011,11/17/2011,11/18/2011,11/21/2011,11/22/2011,11/23/2011,11/25/2011,11/28/2011,11/29/2011,11/30/2011,
12/9/2011,12/12/2011,12/13/2011,12/14/2011,12/15/2011,12/16/2011,12/19/2011,12/20/2011,12/21/2011,12/22/2011)










--
View this message in context: 
http://r.789695.n4.nabble.com/Date-seq-question-tp4313861p4313861.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Date seq question

2012-01-20 Thread R. Michael Weylandt
Try this:

date - 
c(9/22/2011,9/23/2011,9/26/2011,9/27/2011,9/28/2011,9/29/2011,9/30/2011,10/17/2011,
10/18/2011,10/19/2011,10/20/2011,10/21/2011,10/24/2011,10/25/2011,10/26/2011,11/17/2011,11/18/2011,11/21/2011,11/22/2011,11/23/2011,11/25/2011,11/28/2011,11/29/2011,11/30/2011,
12/9/2011,12/12/2011,12/13/2011,12/14/2011,12/15/2011,12/16/2011,12/19/2011,12/20/2011,12/21/2011,12/22/2011)

library(lubridate)
date - as.Date(date, format = %m/%d/%Y) # Convert character to an actual date
day(date) - 1; # Shift every date to the first day of its month
date - 1 # And go one day previous

Michael

On Fri, Jan 20, 2012 at 12:12 PM, cameron raymond...@invesco.com wrote:
 Can anyone please help me with this?
 I have a list of business dates.  What I want is to have last day of last
 month and paste them on next month.

 What i have                        What i want
 5725 2011-09-22
 5726 2011-09-23
 5727 2011-09-26
 5728 2011-09-27
 5729 2011-09-28
 5730 2011-09-29
 5731 2011-09-30
 5742 2011-10-17                 2011-09-30
 5743 2011-10-18                 2011-09-30
 5744 2011-10-19                 2011-09-30
 5745 2011-10-20                 2011-09-30
 5746 2011-10-21                 2011-09-30
 5747 2011-10-24                 2011-09-30
 5748 2011-10-25                 2011-09-30
 *5749 2011-10-26*                 2011-09-30
 5765 2011-11-17                 2011-10-26
 5766 2011-11-18                 2011-10-26
 5767 2011-11-21                 2011-10-26
 5768 2011-11-22                 2011-10-26
 5769 2011-11-23                 2011-10-26
 5770 2011-11-25                 2011-10-26
 5771 2011-11-28                 2011-10-26
 5772 2011-11-29                 2011-10-26
 *5773 2011-11-30*                 2011-10-26
 5780 2011-12-09                 2011-11-30
 5781 2011-12-12                 2011-11-30
 5782 2011-12-13                 2011-11-30
 5783 2011-12-14                 2011-11-30
 5784 2011-12-15                 2011-11-30
 5785 2011-12-16                 2011-11-30
 5786 2011-12-19                 2011-11-30
 5787 2011-12-20                 2011-11-30
 5788 2011-12-21                 2011-11-30
 5789 2011-12-22                 2011-11-30

 date -
 c(9/22/2011,9/23/2011,9/26/2011,9/27/2011,9/28/2011,9/29/2011,9/30/2011,10/17/2011,
 10/18/2011,10/19/2011,10/20/2011,10/21/2011,10/24/2011,10/25/2011,10/26/2011,11/17/2011,11/18/2011,11/21/2011,11/22/2011,11/23/2011,11/25/2011,11/28/2011,11/29/2011,11/30/2011,
 12/9/2011,12/12/2011,12/13/2011,12/14/2011,12/15/2011,12/16/2011,12/19/2011,12/20/2011,12/21/2011,12/22/2011)










 --
 View this message in context: 
 http://r.789695.n4.nabble.com/Date-seq-question-tp4313861p4313861.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@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@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.


Re: [R] Date seq question

2012-01-20 Thread Brian Diggs

On 1/20/2012 9:12 AM, cameron wrote:

Can anyone please help me with this?
I have a list of business dates.  What I want is to have last day of last
month and paste them on next month.

What i haveWhat i want
5725 2011-09-22
5726 2011-09-23
5727 2011-09-26
5728 2011-09-27
5729 2011-09-28
5730 2011-09-29
5731 2011-09-30
5742 2011-10-17 2011-09-30
5743 2011-10-18 2011-09-30
5744 2011-10-19 2011-09-30
5745 2011-10-20 2011-09-30
5746 2011-10-21 2011-09-30
5747 2011-10-24 2011-09-30
5748 2011-10-25 2011-09-30
*5749 2011-10-26* 2011-09-30
5765 2011-11-17 2011-10-26
5766 2011-11-18 2011-10-26
5767 2011-11-21 2011-10-26
5768 2011-11-22 2011-10-26
5769 2011-11-23 2011-10-26
5770 2011-11-25 2011-10-26
5771 2011-11-28 2011-10-26
5772 2011-11-29 2011-10-26
*5773 2011-11-30* 2011-10-26
5780 2011-12-09 2011-11-30
5781 2011-12-12 2011-11-30
5782 2011-12-13 2011-11-30
5783 2011-12-14 2011-11-30
5784 2011-12-15 2011-11-30
5785 2011-12-16 2011-11-30
5786 2011-12-19 2011-11-30
5787 2011-12-20 2011-11-30
5788 2011-12-21 2011-11-30
5789 2011-12-22 2011-11-30


Reformatting the data you gave:


date- c(9/22/2011, 9/23/2011, 9/26/2011, 9/27/2011,
9/28/2011, 9/29/2011, 9/30/2011, 10/17/2011, 10/18/2011,
10/19/2011, 10/20/2011, 10/21/2011, 10/24/2011,
10/25/2011, 10/26/2011, 11/17/2011, 11/18/2011, 11/21/2011,
11/22/2011, 11/23/2011, 11/25/2011, 11/28/2011, 11/29/2011,
11/30/2011, 12/9/2011, 12/12/2011, 12/13/2011, 12/14/2011,
12/15/2011, 12/16/2011, 12/19/2011, 12/20/2011,
12/21/2011, 12/22/2011)


Here is a solution using plyr and zoo:

library(plyr)
library(zoo)

DF - data.frame(date)
DF - mutate(DF,
 dt = as.Date(date, format=%m/%d/%Y),
 ym = as.yearmon(dt))
lastday - ddply(DF, .(ym), summarise, last=max(dt))
lastday - mutate(lastday, ym=ym+(1/12))
merge(DF, lastday, all.x=TRUE)

I first turn the strings into dates, and then into yearmons (year-month) 
which is what I need zoo for.  Use ddply to get the last date within 
each year-month group.  Add one month to the yearmon summaries to go to 
the next month.  Then merge these two back together, implicitly by the 
yearmon.  You could then delete any of the intermediate result columns 
if you like.


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health  Science University

__
R-help@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.


Re: [R] Date seq question

2012-01-20 Thread cameron
Thanks Michael and Brian

Thanks for your time.

--
View this message in context: 
http://r.789695.n4.nabble.com/Date-seq-question-tp4313861p4314793.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.