[R] Convert Numeric (20090101) to Date
pdalgd at gmail.com
Wed Mar 7 08:28:36 CET 2012
On Mar 7, 2012, at 02:42 , jim holtman wrote:
> forgot what you asked for:
>> format(x, format = "%d-%m-%Y")
>  "01-01-2009"
I don't think that was quite what was asked for. Original question was to convert something read as numeric to a date. For that, you need to first convert to a character string by throwing an as.character into the mix:
> as.Date(20091231, format="%Y%m%d")
Error in as.Date.numeric(20091231, format = "%Y%m%d") :
'origin' must be supplied
> as.Date(as.character(20091231), format="%Y%m%d")
> format(as.Date(as.character(20091231), format="%Y%m%d"),"%d-%m-%Y")
(For other date formats, you may have to tackle leading zeros as well.)
A better idea could be to tell read.fwf to read the column as character (use colClasses).
(AFAIK, with SAS you also get to jump through a few hoops to convert dates that was accidentally read as numbers. The effective way is to specify a format when reading data.)
> On Tue, Mar 6, 2012 at 8:40 PM, jim holtman <jholtman at gmail.com> wrote:
>> try this:
>>> x <- as.POSIXct("20090101", format = "%Y%m%d")
>>  "2009-01-01 EST"
>> On Tue, Mar 6, 2012 at 7:38 PM, RHelpPlease <rrumple at trghcsolutions.com> wrote:
>>> Hi there,
>>> Does it exist where R can convert a numeric date (20090101) to a "proper"
>>> date format? (Ideally dd-mm-yyyy)
>>> Original data (in this case) is in .DAT format. I read the multi-column
>>> data with the read.fwf function, where I specified the column width for the
>>> eight digit date (example above). After the .DAT data is read-in &
>>> formatted in R, it is to be exported to Excel.
>>> I understand that with the as.Date function, 20090101 is understood as the
>>> number of days from the R origin date.
>>> I read that SAS has the capability to convert 20090101 to a date, so I'm
>>> hoping R does as well. Conversion to a date in Excel does not work.
>>> Help in this matter is most appreciated!
>>> View this message in context: http://r.789695.n4.nabble.com/Convert-Numeric-20090101-to-Date-tp4451859p4451859.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>> R-help at r-project.org mailing list
>>> 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.
> 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 at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help