Normally, I use chron to represent date/time objects. However, I need to use some functions that work with the POSIX format, so I am trying to go from chron to POSIXct. Using as.POSIXct() seems to work but the result is in localtime instead of GMT (the original data is in GMT).
x <- chron(dates="05/12/15", times="12:30:45")
as.POSIXct(x, tz="GMT")
"2015-05-12 13:30:45 BST"
what I want is:
"2015-05-12 12:30:45 GMT"
but I can't find a way to obtain it.
strptime() won't work because the original input is not a string, but a chron object. Of course I could go from a chron object to a character string and then to POSIXct but it seems a bit convoluted way to do it.
I suppose I could force my R session to use GMT with Sys.timezone(), but I'd prefer not to. Any other suggestion? Thank you.
Just try:
x <- chron(dates="05/12/15", times="12:30:45")
y<-as.POSIXct(x)
attr(y,"tzone")<-"GMT"
y
#[1] "2015-05-12 12:30:45 GMT"
Related
I have data "A" in the format chr "5/7/2021 15:15". I would like to convert it to a format which R will recognize. (It is giving me errors when I try to plot, for instance, which leads me to believe it needs to be reformatted.)
Here is the format "B" I would like to achieve. R seems to like this ok, so I might as well match it (?):
POSIXct, format: "2021-8-11 16:00:00". I am not sure if the seconds are needed, and they do not exist in data "A" so the seconds could be omitted. If R doesn't care then I don't either. The timezone is UTC.
How do I do it? I have tried a couple things, including:
CTD_datetime_UTC <- as.POSIXct(CTD$Date.and.Time, tz = "UTC").
You can use strptime from base R. But there are many parsers for dates...
Assuming the format is "day/month/year" (example is not unambiguous, could also be "month/day/year")
strptime("5/7/2021 15:15", "%d/%m/%Y %H:%M", tz = "UTC")
Returns:
[1] "2021-07-05 15:15:00 UTC"
Using parsedate
library(parsedate)
parse_date("5/7/2021 15:15")
[1] "2021-05-07 15:15:00 UTC"
Is there a way to extract the timezone or format the timezone part of a datetime object, in the form of, say "+530" instead of "IST" or "Asia/Kolkata"?
(Need this as it is the ISO 8601 format used in javascript)
Example:
as.POSIXct(1499773898,tz="Asia/Kolkata",origin="1970-01-01")
[1] "2017-07-11 17:21:38 IST"
Instead, I'd like to maybe specify the format argument in as.POSIXct, so that the output looks something like this:
[1] "2017-07-11 17:21:38 +530"
Or a function which can pull out the timezone offset in this manner:
timezone_offset("2017-07-11 17:21:38 IST")
[1] "+530"
Does lubridate or any other package have the capability to do this?
You can do both with format, but note that result is character string, no longer POSIXct object.
x <- as.POSIXct(1499773898,tz="Asia/Kolkata",origin="1970-01-01")
"2017-07-11 17:21:38 IST"
e.g., Show timestamp in ISO 8601:
format(x, "%Y-%m-%dT%H:%M:%S%z")
"2017-07-11T17:21:38+0530"
e.g., Show just offset from UTC:
format(x, "%z")
"+0530"
Note that for operations in R this has little consequence because all POSIXct objects are stored internally as numeric in UTC; seconds from 1970-01-01 00:00:00.
To write POSIXct timestamps in ISO 8601 to file, you can use format as described above or fwrite function in data.table, which does so by default (see dateTimeAs argument).
I need to convert a time in character "01:15:55 AM" to time.
I was thinking of using chron library, however I am not sure how to do the format.
chron(times = "01:15:55 AM", format = "%I:%M:%S %p")
Error in parse.format(format) : unrecognized format %I:%M:%S %p
Any ideas? I'm happy to try other libraries
One option is to use strptime to parse, and then format the result so you can make it into a nice chron::times object:
library(chron)
times(format(strptime("01:15:55 PM", format = "%I:%M:%S %p"), "%H:%M:%S"))
# [1] 13:15:55
testtime<-("2013-07-21 02:00:01 PM")
library(lubridate)
ymd_hms(testtime)
[1] "2013-07-21 14:00:01 UTC"
The canonical way of dealing with date and time objects in R is using the POSIX functions, such as:
> (time.POSIX<-strptime("01:15:55 AM", format = "%I:%M:%S %p"))
[1] "2016-03-31 01:15:55 BRT"
> class(time.POSIX)
[1] "POSIXlt" "POSIXt"
The strptime function, along with all others that work with POSIX dates, such as format (for printing) accept the format string that you're using.
The problem here is that this data is not a time per se, but a date and a time, and the date part is implicitly created as "today". So you can create this time objects in a fixed reference date, say, 2000-01-01.
On the other hand, if you want to use the chron package... they are not compliant with these format specifications. chron manual only specifies m, d, y, h, m and s.
You might have to write a wrapper that understands AM/PM and fixes it.
I have a simple question regarding R's lubridate package. I've a series of timestamps in seconds since epoch. I want to convert this to YYYY-MM-DD-HH format. In base R, I can do something like this to first convert it to a date format
> x = as.POSIXct(1356129107,origin = "1970-01-01",tz = "GMT")
> x
[1] "2012-12-21 22:31:47 GMT"
Note the above just converts it to a date format, not the YYYY-MM-DD-HH format. How would I do this in lubridate? How would I do it using base R?
Thanks much in advance
lubridate has an as_datetime() that happens to have UNIX epoch time as the default origin time to make this really simple:
> as_datetime(1356129107)
[1] "2012-12-21 22:31:47 UTC"
more details can be found here: https://rdrr.io/cran/lubridate/man/as_date.html
Dirk is correct. However, if you are intent on using lubridate functions:
paste( year(dt), month(dt), mday(dt), hour(dt) sep="-")
If on the other hand you want to handle the POSIXct objects the way they were supposed to be used then this should satisfy:
format(x, format="%Y-%m-%d-%H")
I use the lubridate solution provided by #leerssej
But in case anyone prefers #IRTFM's solution in base R, but also wants minutes and seconds, here's an example of how to do that:
as.POSIXct("2019-03-15 16:17:42" , format="%Y-%m-%d %H:%M:%OS")
My code:
axis.Date(1,sites$date, origin="1970-01-01")
Error:
Error in as.Date.numeric(x) : 'origin' must be supplied
Why is it asking me for the origin when I supplied it in the above code?
I suspect you meant:
axis.Date(1, as.Date(sites$date, origin = "1970-01-01"))
as the 'x' argument to as.Date() has to be of type Date.
As an aside, this would have been appropriate as a follow-up or edit of your previous question.
My R use 1970-01-01:
>as.Date(15103, origin="1970-01-01")
[1] "2011-05-09"
and this matches the calculation from
>as.numeric(as.Date(15103, origin="1970-01-01"))
So generally this has been solved, but you might get this error message because the date you use is not in the correct format.
I know this is an old post, but whenever I run this I get NA all the way down my date column. My dates are in this format 20150521 – NealC Jun 5 '15 at 16:06
If you have dates of this format just check the format of your dates with:
str(sides$date)
If the format is not a character, then convert it:
as.character(sides$date)
For as.Date, you won't need an origin any longer, because this is supplied for numeric values only. Thus you can use (assuming you have the format of NealC):
as.Date(as.character(sides$date),format="%Y%m%d")
I hope this might help some of you.
Another option is the lubridate package:
library(lubridate)
x <- 15103
as_date(x, origin = lubridate::origin)
"2011-05-09"
y <- 1442866615
as_datetime(y, origin = lubridate::origin)
"2015-09-21 20:16:55 UTC"
From the docs:
Origin is the date-time for 1970-01-01 UTC in POSIXct format. This date-time is the origin for the numbering system used by POSIXct, POSIXlt, chron, and Date classes.
If you have both date and time information in the numeric value, then use as.POSIXct. Data.table package IDateTime format is such a case. If you use fwrite to save a file, the package automatically converts date-times to idatetime format which is unix time. To convert back to normal format following can be done.
Example: Let's say you have a unix time stamp with date and time info: 1442866615
> as.POSIXct(1442866615,origin="1970-01-01")
[1] "2015-09-21 16:16:54 EDT"
by the way, the zoo package, if it is loaded, overrides the base as.Date() with its own which, by default, provides origin="1970-01-01".
(i mention this in case you find that sometimes you need to add the origin, and sometimes you don't.)