understanding dates/times (POSIXc and POSIXct) in R
strptime
returns class POSIXlt
, you need POSIXct
in the data frame:
R> class(strptime("2009-09-30 10:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC"))
[1] "POSIXt" "POSIXlt"
R> class(as.POSIXct("2009-09-30 10:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC"))
[1] "POSIXt" "POSIXct"
Class POSIXct
represents the (signed) number of seconds since the beginning of
1970 as a numeric vector. Class POSIXlt
is a named list of vectors representing sec, min, hour, mday, mon, year, etc.
R> unclass(strptime("2009-09-30 10:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC"))
$sec
[1] 0
$min
[1] 0
$hour
[1] 10
$mday
[1] 30
$mon
[1] 8
$year
[1] 109
$wday
[1] 3
$yday
[1] 272
$isdst
[1] 0
attr(,"tzone")
[1] "UTC"
R> unclass(as.POSIXct("2009-09-30 10:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC"))
[1] 1.254e+09
attr(,"tzone")
[1] "UTC"
Problem with changing format of date and time in R
From the manual help(strftime)
:
If the specified time is invalid (for example ‘"2010-02-30 08:00"’) all the components of the result are ‘NA’.
Furthermore, if you want to manipulate your output format of a date you need the date stored as a Date-class (illustration of @izyda's comments).
First, reformat your date to make it easier to manipulate:
data <- "20170929 20:59:56.149"
dat_new <- paste( paste( substr(data, 1, 4),
substr(data, 5, 6), substr(data, 7, 8), sep="-" ),
substr(data, 10,nchar(data)) )
dat_new
[1] "2017-09-29 20:59:56.149"
Then, change the class to Date:
dat_cor <- as.POSIXct( dat_new, tz="GMT" )
dat_cor
[1] "2017-09-29 20:59:56 GMT"
class(dat_new)
[1] "character"
class(dat_cor)
[1] "POSIXct" "POSIXt"
Finally, choose your output format:
strftime( dat_cor, format="%m/%d/%Y %H:%M:%OS3", tz="GMT" )
[1] "09/29/2017 20:59:56.148"
# or
strftime( dat_cor, format="%Y-%d-%m %H:%M:%OS3", tz="GMT" )
[1] "2017-29-09 20:59:56.148"
Given a sequence of POSIXct which include date and time how do I calculate the mean of times only (excluding days)?
Here is one way using some help from lubridate
library.
library(lubridate)
format(sampledat, '%T') %>%
hms %>%
period_to_seconds() %>%
mean %>%
as.POSIXct(origin = '1970-01-01', tz = 'UTC') %>%
format('%T')
We extract the time part from sampledat
, convert it to period object, convert that into number of seconds, take mean
of it and display it in "HMS" format.
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")
posixct time not understanding the '60s
Two-digit years are ambiguous. You can add a "19" using regex then parse with %Y
instead of %y
library(tidyverse)
discharge %>%
rownames_to_column(var="date") %>%
as_tibble() %>%
mutate(date = strptime(sub("^(\\d+/\\d+/)(\\d+)$", "\\119\\2", date),
format = "%m/%d/%Y"))
#> # A tibble: 261 x 2
#> date Original
#> <dttm> <int>
#> 1 1963-04-01 00:00:00 1100
#> 2 1963-05-01 00:00:00 1030
#> 3 1963-06-01 00:00:00 982
#> 4 1963-07-01 00:00:00 703
#> 5 1963-08-01 00:00:00 587
#> 6 1963-09-01 00:00:00 512
#> 7 1963-10-01 00:00:00 606
#> 8 1963-11-01 00:00:00 667
#> 9 1963-12-01 00:00:00 1010
#> 10 1964-01-01 00:00:00 1400
#> # ... with 251 more rows
Created on 2022-04-28 by the reprex package (v2.0.1)
Related Topics
More Efficient Strategy for Which() or Match()
How to Build Multiclass Svm in R
How to Change Node and Link Colors in R Googlevis Sankey Chart
Reproduce a 'The Economist' Chart with Dual Axis
Error in R Gbm Function When Cv.Folds > 0
Handling Latex Backslashes in Xtable
Object.Size() Reports Smaller Size Than .Rdata File
Generating a Very Large Matrix of String Combinations Using Combn() and Bigmemory Package
Combine Lists While Overriding Values with Same Name in R
Creating a Monthly/Yearly Calendar Image with Ggplot2
R Data.Table: Subgroup Weighted Percent of Group
How to Prevent Objects from Automatically Loading When I Open Rstudio
Ggplot2 Bar Plot with Two Categorical Variables
Custom Fill Color in Ggvis (And Other Options)
How to Pass the "..." Parameters in the Parent Function to Its Two Children Functions in R