Hello, I have a date in the format Year-Month Name (e.g. 1990-January) and R classes it as a character. I want to convert this character into a date format, but when I try as.Date(1990-January, "%Y-%B"), I get back NA. The function strptime also gives me NA back. Thanks. [[alternative HTML version deleted]]
If you want the vector to be a Date you need to specify a date at least down to the day. Otherwise the date is not well defined and becomes <NA> as you noted. Perhaps the easiest thing is to give it a particular day of the month, e.g. the first, or the 15 (the "ides"), or ...> x <- as.Date(paste("1990-January", 1, sep="-"), format = "%Y-%B-%d") > x[1] 1990-01-01 Now if you want to display the date suppressing the dummy day, you can> y <- format(x, "%Y-%B") > y[1] "1990-January" Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bob Roberts Sent: Tuesday, 31 March 2009 5:14 PM To: r-help at r-project.org Subject: [R] Convert Character to Date Hello, I have a date in the format Year-Month Name (e.g. 1990-January) and R classes it as a character. I want to convert this character into a date format, but when I try as.Date(1990-January, "%Y-%B"), I get back NA. The function strptime also gives me NA back. Thanks. [[alternative HTML version deleted]] ______________________________________________ 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.
The yearmon class in the zoo package can represent year/months:> library(zoo) > ym <- as.yearmon("1990-January", "%Y-%B"); ym[1] "Jan 1990"> as.Date(ym)[1] "1990-01-01" On Tue, Mar 31, 2009 at 3:14 AM, Bob Roberts <quagmire54321 at yahoo.com> wrote:> Hello, > ? I have a date in the format Year-Month Name (e.g. 1990-January) and R classes it as a character. I want to convert this character into a date format, but when I try as.Date(1990-January, "%Y-%B"), I get back NA. The function strptime also gives me NA back. Thanks. > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Just to correct/expand/clarify the parenthetical below (how often is there a chance to correct or clarify something posted by Bill Venables?), the ides are the 15th of March, May, July, and October, but the 13th of the other months. So if you want to use the ides as the date to use, you will need a vector rather than the scalar 15, if you use 15, then it will only be the ides of some months. I wonder if the roman calendar was created on kalends April, but no one got the joke. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Bill.Venables at csiro.au > Sent: Tuesday, March 31, 2009 1:29 AM > To: quagmire54321 at yahoo.com; r-help at r-project.org > Subject: Re: [R] Convert Character to Date > > If you want the vector to be a Date you need to specify a date at least > down to the day. Otherwise the date is not well defined and becomes > <NA> as you noted. > > Perhaps the easiest thing is to give it a particular day of the month, > e.g. the first, or the 15 (the "ides"), or ... > > > x <- as.Date(paste("1990-January", 1, sep="-"), format = "%Y-%B-%d") > > x > [1] 1990-01-01 > > Now if you want to display the date suppressing the dummy day, you can > > > y <- format(x, "%Y-%B") > > y > [1] "1990-January" > > > Bill Venables > http://www.cmis.csiro.au/bill.venables/ > > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Bob Roberts > Sent: Tuesday, 31 March 2009 5:14 PM > To: r-help at r-project.org > Subject: [R] Convert Character to Date > > Hello, > I have a date in the format Year-Month Name (e.g. 1990-January) and > R classes it as a character. I want to convert this character into a > date format, but when I try as.Date(1990-January, "%Y-%B"), I get back > NA. The function strptime also gives me NA back. Thanks. > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
I stand corrected, with thanks! Gee you learn some fascinating stuff on this mailing list....just fascinating... :-) W. Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: Greg Snow [mailto:Greg.Snow at imail.org] Sent: Thursday, 2 April 2009 5:58 AM To: Venables, Bill (CMIS, Cleveland); quagmire54321 at yahoo.com; r-help at r-project.org Subject: RE: [R] Convert Character to Date Just to correct/expand/clarify the parenthetical below (how often is there a chance to correct or clarify something posted by Bill Venables?), the ides are the 15th of March, May, July, and October, but the 13th of the other months. So if you want to use the ides as the date to use, you will need a vector rather than the scalar 15, if you use 15, then it will only be the ides of some months. I wonder if the roman calendar was created on kalends April, but no one got the joke. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Bill.Venables at csiro.au > Sent: Tuesday, March 31, 2009 1:29 AM > To: quagmire54321 at yahoo.com; r-help at r-project.org > Subject: Re: [R] Convert Character to Date > > If you want the vector to be a Date you need to specify a date at least > down to the day. Otherwise the date is not well defined and becomes > <NA> as you noted. > > Perhaps the easiest thing is to give it a particular day of the month, > e.g. the first, or the 15 (the "ides"), or ... > > > x <- as.Date(paste("1990-January", 1, sep="-"), format = "%Y-%B-%d") > > x > [1] 1990-01-01 > > Now if you want to display the date suppressing the dummy day, you can > > > y <- format(x, "%Y-%B") > > y > [1] "1990-January" > > > Bill Venables > http://www.cmis.csiro.au/bill.venables/ > > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Bob Roberts > Sent: Tuesday, 31 March 2009 5:14 PM > To: r-help at r-project.org > Subject: [R] Convert Character to Date > > Hello, > I have a date in the format Year-Month Name (e.g. 1990-January) and > R classes it as a character. I want to convert this character into a > date format, but when I try as.Date(1990-January, "%Y-%B"), I get back > NA. The function strptime also gives me NA back. Thanks. > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.