Is there easy way to convert numeric to date? For example: If I have, a = as.Date("2010-03-04") And, if run> as.numeric(a)[1] 14672 Is there any function that I can use to convert 14672 back to the date. Thx in advance, Brijesh -------------------------------------------------------------------------- This message w/attachments (message) may be privileged, ...{{dropped:30}}
On Wed, 10 Mar 2010, GULATI, BRIJESH (Global Markets FF&O NY) wrote:> Is there easy way to convert numeric to date? > > For example: > > If I have, > > a = as.Date("2010-03-04") > > And, if run > >> as.numeric(a) > [1] 14672 > > Is there any function that I can use to convert 14672 back to the date.You can use as.Date(). In base R, you additionally need to specify origin = "1970-01-01" because that is what is used in R. If you load the package "zoo" then the default is set automatically: R> a <- as.Date("2010-03-04") R> a [1] "2010-03-04" R> b <- as.numeric(a) R> b [1] 14672 R> as.Date(b) Error in as.Date.numeric(b) : 'origin' must be supplied R> as.Date(b, origin = "1970-01-01") [1] "2010-03-04" R> library("zoo") R> as.Date(b) [1] "2010-03-04" hth, Z> Thx in advance, > Brijesh > > -------------------------------------------------------------------------- > This message w/attachments (message) may be privileged, ...{{dropped:30}} > > ______________________________________________ > 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. >
On Mar 10, 2010, at 3:29 PM, GULATI, BRIJESH (Global Markets FF&O NY) wrote:> Is there easy way to convert numeric to date? > > For example: > > If I have, > > a = as.Date("2010-03-04") > > And, if run > >> as.numeric(a) > [1] 14672 > > Is there any function that I can use to convert 14672 back to the date. > > Thx in advance, > Brijesh> as.Date(14672, origin = "1970-01-01")[1] "2010-03-04" Of course, you would have to know that R stores dates internally as the number of days since 1970-01-01, which is curiously not defined in the help page for ?as.Date, but is on ?Date, which is in the See Also of the former. HTH, Marc Schwartz
numToPOSIXct <- function(v) { now <- Sys.time() Epoch <- now - as.numeric(now) Epoch + v } Try this - where v is your numeric version of date -- View this message in context: http://n4.nabble.com/Numeric-to-Date-tp1588108p1588149.html Sent from the R help mailing list archive at Nabble.com.