I have a column with seconds. The start date is 09/01/2017 01:37:33.
I would like to replace the seconds with the date based on the calculations (taking into account the start date). But I couldn't find any answer to this question... Can someone help me, please?
Convert to POSIXct and add the number of seconds. seconds can be a vector of seconds.
seconds <- 2
as.POSIXct("09/01/2017 01:37:33", format = "%m/%d/%Y %H:%M:%S") + seconds
## [1] "2017-09-01 01:37:35 EDT"
We convert the start date to as.Posixct and set it as origin when converting seconds to date -
origin <- as.POSIXct("09/01/2017 01:37:33", format = "%m/%d/%Y %H:%M:%S")
# "2017-09-01 01:37:33 EDT"
seconds <- 1:5
as.POSIXct(seconds, origin = origin)
[1] "2017-08-31 21:37:34 EDT" "2017-08-31 21:37:35 EDT" "2017-08-31 21:37:36 EDT" "2017-08-31 21:37:37 EDT" "2017-08-31 21:37:38 EDT"
Related
I can't figure out how to instruct R to change the columns "created_at" and "deadline" into date format. I don't recognize a date/time pattern in the numbers.
Assuming the numbers represent seconds from some origin (perhaps 1970-01-01 00:00:00), you can use as.POSIXct():
as.POSIXct(1240456019, origin="1970-01-01 00:00:00")
# [1] "2009-04-22 23:06:59 EDT"
as.POSIXct(1242467940, origin="1970-01-01 00:00:00")
# [1] "2009-05-16 05:59:00 EDT"
To do this to the entire variable, do:
kickstart$created_at = as.POSIXct(kickstart$created_at, origin="1970-01-01 00:00:00")
kickstart$deadline = as.POSIXct(kickstart$deadline, origin="1970-01-01 00:00:00")
Is there a way to convert Sys.time to New york time zone in R?. I tried with following, but did not work
as.POSIXct(Sys.time(), tz="EDT")
Can anyone help?
When you call Sys.time(), the time zone is already included in the string:
Sys.time()
#> [1] "2020-09-18 08:56:56 BST"
If you want to convert that time to a different timezone, you can set its "tzone" attribute:
`attr<-`(as.POSIXct(Sys.time()), "tzone", "EST")
#> [1] "2020-09-18 02:57:23 EST"
My system doesn't recognise "EDT" as a named time zone. I have to do:
`attr<-`(as.POSIXct(Sys.time()), "tzone", "America/New_York")
#> [1] "2020-09-18 03:59:55 EDT"
You can display time in any time-zone using format :
time <- Sys.time()
format(time, tz="America/New_York",usetz=TRUE)
#[1] "2020-09-18 03:59:39 EDT"
#this works too
format(time, tz="US/Eastern",usetz=TRUE)
#[1] "2020-09-18 03:59:39 EDT"
I have data value
dput(a)
"1/3/2019 15:59"
I need to round the time to to the next hour. I need this date to be "1/3/2019 16:00"?
How can I do this?
We can use lubridate dmy_hm to convert to datetime object and then use ceiling_date to convert it to next hour.
library(lubridate)
ceiling_date(dmy_hm("1/3/2019 15:59"), "hour")
#[1] "2019-03-01 16:00:00 UTC"
Use round.POSIXt. No packages are used.
x <- as.POSIXct("1/3/2019 15:59", format = "%m/%d/%Y %H:%M")
round(x + 3600/2 - !(as.numeric(x) %% 3600), "hours")
## [1] "2019-01-03 16:00:00 EST"
I have col of number of days since 1970-01-01 00:00:00 UTC that I want to convert to date and time UTC.
I tried this:
z <- Sys.time()
j=floor(unclass(z)/86400) # the number of days since 1970-01-01 (UTC)
as.POSIXct(j, origin = "1970-01-01")
[1] "1970-01-01 05:53:22 CET"
But it is not correct. any idea?
You have to pass to the function as.POSIXct the number of seconds, try this code
j<-17148.5625000000
as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC")
[1] "2016-12-13 13:30:00 UTC"
If you want only the day in output
as.Date(as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC"))
[1] "2016-12-13"
I would like to convert the following dates
dates <-c(1149318000L, 1151910000L, 1154588400L, 1157266800L, 1159858800L, 1162540800L)
into date and time format
I don't know the origin of the date but I know that
1146685218 = 2006/05/03 07:00:00
** Update 1 **
I have sorted the unformatted dates and replace the sample above with a friendly sequence but I have real dates. I was thinking of using the the above key as origin, but It does not seem to work.
let
seconds_in_days <- 3600*24
(dates[2]-dates[1])/seconds_in_days
## [1] 30
If you know
1146685218 = 2006/05/03 07:00:00
then just make that the origin
dates <- c(1149318000L, 1151910000L, 1154588400L, 1157266800L, 1159858800L, 1162540800L)
orig.int <- 1146685218
orig.date <- as.POSIXct("2006/05/03 07:00:00", format="%Y/%m/%d %H:%M:%S")
as.POSIXct(dates-orig.int, origin=orig.date)
# [1] "2006-06-02 18:19:42 EDT" "2006-07-02 18:19:42 EDT" "2006-08-02 18:19:42 EDT"
# [4] "2006-09-02 18:19:42 EDT" "2006-10-02 18:19:42 EDT" "2006-11-02 18:19:42 EST"
This works assuming your "date" values are the number of seconds since a particular date/time which is how POSIXt stores it's date/time values.