[R] Categorizing by month
Ben Tupper
btupper at bigelow.org
Sat Mar 28 01:00:16 CET 2015
Hi,
On Mar 27, 2015, at 12:06 PM, lychang <lychang at emory.edu> wrote:
> Hi everyone,
>
> I'm trying to categorize by month in R. How can I do this if my dates are in
> date/month/year form?
>
I'm not sure about the date form you describe, but if you have the dates as POSIXct you can extract the month as character and categorize with that.
x <- seq(from = as.POSIXct("2000/1/1", format="%Y/%m/%d"), to = as.POSIXct("2009/12/1", format="%Y/%m/%d"), by = 'month')
mon <- format(x, '%m')
xx <- split(x, mon)
str(xx)
List of 12
$ 01: POSIXct[1:10], format: "2000-01-01" "2001-01-01" "2002-01-01" "2003-01-01" ...
$ 02: POSIXct[1:10], format: "2000-02-01" "2001-02-01" "2002-02-01" "2003-02-01" ...
$ 03: POSIXct[1:10], format: "2000-03-01" "2001-03-01" "2002-03-01" "2003-03-01" ...
$ 04: POSIXct[1:10], format: "2000-04-01" "2001-04-01" "2002-04-01" "2003-04-01" ...
$ 05: POSIXct[1:10], format: "2000-05-01" "2001-05-01" "2002-05-01" "2003-05-01" ...
$ 06: POSIXct[1:10], format: "2000-06-01" "2001-06-01" "2002-06-01" "2003-06-01" ...
$ 07: POSIXct[1:10], format: "2000-07-01" "2001-07-01" "2002-07-01" "2003-07-01" ...
$ 08: POSIXct[1:10], format: "2000-08-01" "2001-08-01" "2002-08-01" "2003-08-01" ...
$ 09: POSIXct[1:10], format: "2000-09-01" "2001-09-01" "2002-09-01" "2003-09-01" ...
$ 10: POSIXct[1:10], format: "2000-10-01" "2001-10-01" "2002-10-01" "2003-10-01" ...
$ 11: POSIXct[1:10], format: "2000-11-01" "2001-11-01" "2002-11-01" "2003-11-01" ...
$ 12: POSIXct[1:10], format: "2000-12-01" "2001-12-01" "2002-12-01" "2003-12-01" ...
Does that help?
Ben
> Thanks,
> Lois
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Categorizing-by-month-tp4705173.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
More information about the R-help
mailing list