The date July, 1, 2016 1:15pm and 43 seconds is given to me as the string 160701131543.
I have an entire column in my data frame of this date time. How should I go about parsing this column into usable data.
You can use the as.POSIXct function and specify the format, in your case the format is year, month, day, hour, minute, second. Read more about formatting date and time data on the ?strptime help page.
as.POSIXct("160701131543", format = "%y%m%d%H%M%S")
[1] "2016-07-01 13:15:43 EDT"
The timezone can be changed with the 'tz' parameter.
Here is another option with lubridate. The default tz is "UTC". It can be changed by specifying tz
library(lubridate)
ymd_hms("160701131543")
#[1] "2016-07-01 13:15:43 UTC"
Related
I have to convert on R a column with julian dates with decimal part (as parts of the day) to date and hour.
I tried this function :
as.Date(10625.15, origin=as.Date("1990-01-01 00:00:00"))
But it only gave me the date without the times :
"2019-02-02"
Someone can help me to resolve it ? Thanks in advance!!
You used as.Date and it returned a date, exactly what it is designed to do (ref: ?as.Date says it will return an object of class "Date"). Fortunately, it returns a fractional date:
dput(as.Date(10625.15, origin=as.Date("1990-01-01 00:00:00")))
# structure(17930.15, class = "Date")
### ^^^ yay! we have fraction
so we can wrap it in as.POSIXct:
as.POSIXct(as.Date(10625.15, origin=as.Date("1990-01-01 00:00:00")))
# [1] "2019-02-02 22:36:00 EST"
Timezone notwithstanding. .15 of a day is 3.6 hours, so with converting to UTC above, this would show 03:36:00.
One might be tempted to use as.POSIXct in place of as.Date, though realize that 10625.15 is in fractional days, not fractional seconds (which is what as.POSIXct will expect). To do that, you need to convert from "days" to "seconds":
as.POSIXct(86400*10625.15, origin=as.Date("1990-01-01 00:00:00"))
# [1] "2019-02-02 22:36:00 EST"
I obtained a time string looking like this:
201902041502, containing year, month, day, hour and minute.
Now I want to reformat this string into the german date-time format like this: 04.02.2019 15:02.
I've already tried as.Date and as.POSIXct but it doesnt work and I want to avoid adding seconds to get POSIXct to work.
Thanks in advance! Cheers
You can use strptime to convert the data to a POSIXlt object,
x <- "201902041502"
xd <- strptime(x,"%Y%m%d%H%M")
# [1] "2019-02-04 15:02:00 CET"
and then use strftime to produce your desired format:
strftime(xd, "%d.%m.%Y %H:%M")
# [1] "04.02.2019 15:02"
I have got a value like : 1.566940e+12
which needs to be converted to a date format like 2019-10-06 and an hour on 24 hour scale. I would like to use the anytime package
I suppose this number is in milliseconds.
Using anytime, this is as easy:
anytime::anytime(x = 1.566940e+12/1000)
this is what I get:
"2019-08-27 23:06:40 CEST"
If you specifically want to see only date and hour, you can do so with:
format(anytime::anytime(x = 1.566940e+12/1000), "%Y-%m-%d %H")
"2019-08-27 23"
I have dates and times stored in two columns. The first has the date as "20180831." The time is stored as the number of seconds from midnight; 3am would be stored as 10,800.
I need a combined date time column and am having a hard time with something that should be simple.
I can get the dates in no problem but lubridate "hms" interprets the time field as a period, not a 'time' per se.
I tried converting the date to posix.ct format and then using that as the origin for the time field but posix.ct does not set the time for midnight, instead it sets it for either 1800 or 1900 hours depending on the date. I need it set to midnight for all rows, I don't want any daylight savings time adjustment.
Here's the code:
First I made a function because there are several date and time fields I have to do this for.
mkdate<-function(x){
a<-as.Date(as.character(x),format='%Y%m%d')
a<-as.POSIXct(a)
return(a)
}
df$date<-mkdate(df$date) #applies date making function to date field
df$datetime<-as.POSIXct(df$time,origin=df$date)
I'm sure this has to do with time zones. I'm in Central time zone and I have experimented with adding the "tz" specification into these commands in both the mkdate function and in the time code creating "datetime" column.
I've tried:
tz="America/Chicago"
tz="CST"
tz="UTC"
Help would be much appreciated!
Edited with example:
x<-c(20180831,20180710,20160511,20170105,20180101) #these are dates.
as.POSIXct(as.Date(as.character(x),format="%Y%m%d"))
Above code converts dates to seconds from the Jan 1 1970. I could convert this to numeric and add my 'seconds' value to this field BUT it is not correct. This is what I see instead as the output:
[1] "2018-08-30 19:00:00 CDT" "2018-07-09 19:00:00 CDT" "2016-05-10 19:00:00 CDT" "2017-01-04 18:00:00 CST" "2017-12-31 18:00:00 CST"
Look at the first date - it should be 8/31 but instead it is 8/30. Somewhere in there there is a timezone adjustment taking place. It's moving the clock back 5 or 6 hours because I am on central time. The first entry should be 2018-08-31 00:00:00. I would then convert it to numeric and add the seconds field on and convert back to POSIXct format. I've tried including tz specification all over the place with no luck.
Sys.getlocale("LC_TIME")
returns "English_United States.1252"
I believe the following does what you want.
My locale is the following, so the results are different from yours.
Sys.getlocale("LC_TIME")
#[1] "Portuguese_Portugal.1252"
The difference will be due to the daylight savings time, the summer hour.
As for your problem, all you have to do is to remeber that the objects of class "POSIXct are coded as the number of seconds since an origin, and that origin is usually the midnight of 1970-01-01. So you have to add your seconds since midnight to the seconds of as.Date.
x <- "20180831"
xd <- mkdate(x)
y <- 10800
as.POSIXct(as.integer(xd) + y, origin = "1970-01-01")
#[1] "2018-08-31 04:00:00 BST"
as.POSIXct(as.integer(xd) + y, origin = "1970-01-01", tz = "America/Chicago")
#[1] "2018-08-30 22:00:00 CDT"
There are very many ways to do this:
mktime = function(a, b)modifyList(strptime(a, '%Y%m%d'), list(sec = as.numeric(gsub(',', '', b))))
mktime("20180831",'10,800')
[1] "2018-08-31 03:00:00 PDT"
mktime('20180301','10800')
[1] "2018-03-01 03:00:00 PST"
mktime('20180321','10800')
[1] "2018-03-21 03:00:00 PDT"
Looking at the above code, it does not adjust for the daylight saving time. Irrespective of the date, the seconds still show that it Is 3 AM, including the dates when ST-->DT. This will also take into consideration, your LOCAL timezone.
I have a table (tags) with a column for timestamp (ts), which is formatted as seconds since 1 Jan, 1970 GMT. I'm trying to create a date column that converts the timestamp from seconds to date and time EST.
The suggested code for R was:
tags$date<-strptime(tags$ts, "%Y-%m-%d")
tags$date<-as.POSIXct(tags$date)
But when I do this, tags$date comes up as NA. Any suggestions for what I might be doing wrong? Thanks.
You should us as.POSIXct function instead:
tags$date <- as.POSIXct(tags$ts, origin="1970-01-01", tz="US/New York")
strptime converts between character representations and dates not between timestamp and dates.
Here's a lubridate version. When we use as_datetime we don't need to explicitly specify an origin as it defaults to the desired origin.
lubridate::as_datetime(1507119276, tz='EST')
# [1] "2017-10-04 07:14:36 EST"