Converting numeric time to datetime POSIXct format in R
Try one of these depending on which time zone you want:
t.gmt <- as.POSIXct(3600 * (tdat - 974424), origin = '2011-03-01', tz = "GMT")
t.local <- as.POSIXct(format(t.gmt))
Reconvert numeric date to POSIXct R
As the numeric vector is created from an object with time component, reconversion can also be in the same way i.e. first to POSIXct
and then wrap with as.Date
as.Date(as.POSIXct(date1_num, origin = '1970-01-01'))
#[1] "2017-12-30"
How to convert date & time to numeric by using POSIXct and how to calculate the time?
Additionally, you can also use mdy_hm
function from lubridate
-
library(dplyr)
library(lubridate)
df <- df %>% mutate(across(c(starttime, stoptime), mdy_hm))
In base R, you can use as.POSIXct
df[1:2] <- lapply(df[1:2], as.POSIXct, format = "%m/%d/%Y %H:%M")
Changing the format from numeric to POSIXct object in R
This is not the most elegant solution, but it works
library(stringr)
library(dplyr)
example_data <- c("20121029 0", "20121029 100", "20121029 2100")
# Extract the times
times <- example_data %>% strsplit(. , " ") %>% sapply(. , "[", 2)
times <- str_pad(times, max(nchar(times)), side="right", pad="0")
# Extract just the dates
dates <- example_data %>% substr(., 1, 8)
# Combine date and time and convert to POSIXct
date_time <- paste(dates, times) %>% as.POSIXct(., format="%Y%m%d %H%M")
Convert numeric time format to POSIXlt
Break your numeric values to convert them all to seconds, and add that to a datetime for today:
x <- c(0.00,0.00,0.00,30.19,0.00,25.00,0.00,9.20,0.00,0.00)
as.POSIXct(format(Sys.Date()), tz="Australia/Darwin") + (floor(x)*60) + ((x-floor(x))*100)
# [1] "2017-07-05 00:00:00 ACST" "2017-07-05 00:00:00 ACST" "2017-07-05 00:00:00 ACST" "2017-07-05 00:30:19 ACST"
# [5] "2017-07-05 00:00:00 ACST" "2017-07-05 00:25:00 ACST" "2017-07-05 00:00:00 ACST" "2017-07-05 00:09:20 ACST"
# [9] "2017-07-05 00:00:00 ACST" "2017-07-05 00:00:00 ACST"
This is in Darwin time, ACST
instead of AEST
, but I gather that's what you want.
Convert numeric time in milliseconds to datetime with seconds with decimal value
With lubridate
, using as_datetime
library(lubridate)
as_datetime(time/1000)
[1] "2021-10-08 16:01:17 UTC"
Note that the milliseconds are not print
ed in the console. If we need to print, then format with strftime
or format
(but it will not be a datetime object anymore)
strftime(as_datetime(time/1000), '%Y-%m-%d %H:%M:%OS3')
#[1] "2021-10-08 11:01:17.772"
Or without using any package, just specify it in as.POSIXct
as.POSIXct(time/1000, origin = '1970-01-01')
[1] "2021-10-08 11:01:17 CDT"
How to convert numeric time into date, time format in R
Big fan of parse_date_time
from lubridate
and the timetk
package:
library(tidyverse)
library(lubridate)
library(timetk)
test %>%
mutate(date = parse_date_time(paste(date, str_pad(time, 4, side = "left", pad = "0")), "YmdHM")) %>%
select(-time) %>%
tk_xts(silent = T)
price
1990-01-02 09:30:00 353.40
1990-01-02 09:31:00 353.25
1990-01-02 09:32:00 353.02
1990-01-02 09:33:00 352.97
1990-01-02 09:34:00 352.81
1990-01-02 09:35:00 352.74
Related Topics
Maps, Ggplot2, Fill by State Is Missing Certain Areas on the Map
What Type of Graph Is This? and Can It Be Created Using Ggplot2
How to Use Cast or Another Function to Create a Binary Table in R
R::Ggplot2::Geom_Points: How to Swap Points with Pie Charts
How to Suppress the Creation of a Plot While Calling a Function in R
Matching a Sequence in a Larger Vector
Does the Term "Vectorization" Mean Different Things in Different Contexts
Package "Rvest" for Web Scraping Https Site with Proxy
Ggplot Custom Scale Transformation with Custom Ticks
How to Add a Table to a Ggplot
Find All Unique Values in Column Separated by Comma
Changing Binary Variables to Yes/No
Formatting a Date in R Without Leading Zeros