Re: [R] any way to convert back to DateTime class when accidental conversion to numeric?
A more portable way (that function only works in some versions of R) is as.POSIXct(1317857320, origin=1970-01-01) possibly with a 'tz' argument if you need to restore the timezone. On Wed, 5 Oct 2011, jim holtman wrote: Here is what I use: unix2POSIXct(1317857320) [1] 2011-10-05 19:28:40 EDT unix2POSIXct - function (time) structure(time, class = c(POSIXt, POSIXct)) On Wed, Oct 5, 2011 at 7:38 PM, Mike Williamson this.is@gmail.com wrote: Hi, In short, I would like to know if there is any way to convert a numeric into a date, similar to how strptime() can convert a string to a date time class? There are some functions, etc. which don't work well with dates, and tend to force them into numerics. I understand that the number it spits back is the number of seconds since the beginning of 1970 (see the first few sentences of the Details portion of ?DateTimeClasses). However, it's a bit of a hassle to convert that by hand. I can create a function to do this, and it isn't so hard, but I found it hard to believe such a function didn't already exist, so I wanted to ask the community. As an example, today (Oct 5th 2011 at approximately 4:30pm, Pacific time) is approximately 1317857320 as a numeric, but I would like to know how to go from that number back to the 2011-10-05 16:28:39 PDT date time class which originally generated it. Thanks! Mike --- XKCD http://www.xkcd.com [[alternative HTML version deleted]] __ 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? __ 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595__ 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] any way to convert back to DateTime class when accidental conversion to numeric?
Hi Dr. Ripley, All, Thanks for the succinct advice! Perfectly what I needed! Jeff, Absolutely I agree that this is a dangerous path, and I would never consider doing it for something that needs to be robust. But in 'R' type casting is a bit messed up, so I've come to accept that sometimes something I called a string might become a factor, a date became a numeric, etc. Then I stick in enough catches throughout my functions to deal with it, worst case. It is ugly, but I cannot figure out a way to have tight types and still use 'R'. Yet, 'R' has so many cool functions (and more added every day); I'd be silly to throw the baby out with the bathwater. So my typical best case or robust solution is to write a parent script in python (I simply know python best) that handles all data typing, etc., then call 'R' once I know that everything is clean. In this particular case above where I was asking, this is really for exploratory work. Once I get a solution, I will likely handle typing outside of 'R'. Thanks for the advice! Regards, Mike --- XKCD http://www.xkcd.com On Wed, Oct 5, 2011 at 11:41 PM, Prof Brian Ripley rip...@stats.ox.ac.ukwrote: A more portable way (that function only works in some versions of R) is as.POSIXct(1317857320, origin=1970-01-01) possibly with a 'tz' argument if you need to restore the timezone. On Wed, 5 Oct 2011, jim holtman wrote: Here is what I use: unix2POSIXct(1317857320) [1] 2011-10-05 19:28:40 EDT unix2POSIXct - function (time) structure(time, class = c(POSIXt, POSIXct)) On Wed, Oct 5, 2011 at 7:38 PM, Mike Williamson this.is@gmail.com wrote: Hi, In short, I would like to know if there is any way to convert a numeric into a date, similar to how strptime() can convert a string to a date time class? There are some functions, etc. which don't work well with dates, and tend to force them into numerics. I understand that the number it spits back is the number of seconds since the beginning of 1970 (see the first few sentences of the Details portion of ?DateTimeClasses). However, it's a bit of a hassle to convert that by hand. I can create a function to do this, and it isn't so hard, but I found it hard to believe such a function didn't already exist, so I wanted to ask the community. As an example, today (Oct 5th 2011 at approximately 4:30pm, Pacific time) is approximately 1317857320 as a numeric, but I would like to know how to go from that number back to the 2011-10-05 16:28:39 PDT date time class which originally generated it. Thanks! Mike --- XKCD http://www.xkcd.com [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html 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? __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~**ripley/http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 [[alternative HTML version deleted]] __ 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] any way to convert back to DateTime class when accidental conversion to numeric?
Hi, In short, I would like to know if there is any way to convert a numeric into a date, similar to how strptime() can convert a string to a date time class? There are some functions, etc. which don't work well with dates, and tend to force them into numerics. I understand that the number it spits back is the number of seconds since the beginning of 1970 (see the first few sentences of the Details portion of ?DateTimeClasses). However, it's a bit of a hassle to convert that by hand. I can create a function to do this, and it isn't so hard, but I found it hard to believe such a function didn't already exist, so I wanted to ask the community. As an example, today (Oct 5th 2011 at approximately 4:30pm, Pacific time) is approximately 1317857320 as a numeric, but I would like to know how to go from that number back to the 2011-10-05 16:28:39 PDT date time class which originally generated it. Thanks! Mike --- XKCD http://www.xkcd.com [[alternative HTML version deleted]] __ 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] any way to convert back to DateTime class when accidental conversion to numeric?
Here is what I use: unix2POSIXct(1317857320) [1] 2011-10-05 19:28:40 EDT unix2POSIXct - function (time) structure(time, class = c(POSIXt, POSIXct)) On Wed, Oct 5, 2011 at 7:38 PM, Mike Williamson this.is@gmail.com wrote: Hi, In short, I would like to know if there is any way to convert a numeric into a date, similar to how strptime() can convert a string to a date time class? There are some functions, etc. which don't work well with dates, and tend to force them into numerics. I understand that the number it spits back is the number of seconds since the beginning of 1970 (see the first few sentences of the Details portion of ?DateTimeClasses). However, it's a bit of a hassle to convert that by hand. I can create a function to do this, and it isn't so hard, but I found it hard to believe such a function didn't already exist, so I wanted to ask the community. As an example, today (Oct 5th 2011 at approximately 4:30pm, Pacific time) is approximately 1317857320 as a numeric, but I would like to know how to go from that number back to the 2011-10-05 16:28:39 PDT date time class which originally generated it. Thanks! Mike --- XKCD http://www.xkcd.com [[alternative HTML version deleted]] __ 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? __ 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] any way to convert back to DateTime class when accidental conversion to numeric?
This game you are playing is quite dangerous, and I recommend that you avoid this assiduously. Look at the lubricate library or use the chron or Date classes. Timestamps are rather poor candidates for numeric operations, and the vagaries of timezones are quite a headache. --- Jeff Newmiller The . . Go Live... DCN:jdnew...@dcn.davis.ca.us Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Mike Williamson this.is@gmail.com wrote: Hi, In short, I would like to know if there is any way to convert a numeric into a date, similar to how strptime() can convert a string to a date time class? There are some functions, etc. which don't work well with dates, and tend to force them into numerics. I understand that the number it spits back is the number of seconds since the beginning of 1970 (see the first few sentences of the Details portion of ?DateTimeClasses). However, it's a bit of a hassle to convert that by hand. I can create a function to do this, and it isn't so hard, but I found it hard to believe such a function didn't already exist, so I wanted to ask the community. As an example, today (Oct 5th 2011 at approximately 4:30pm, Pacific time) is approximately 1317857320 as a numeric, but I would like to know how to go from that number back to the 2011-10-05 16:28:39 PDT date time class which originally generated it. Thanks! Mike --- XKCD http://www.xkcd.com [[alternative HTML version deleted]] _ 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. [[alternative HTML version deleted]] __ 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.