日期时间转换和仅提取时间

Date time conversion and extract only time

想要将时间的 class 更改为 POSIXlt 并仅提取时分秒

str(df3$Time)
chr [1:2075259] "17:24:00" "17:25:00" "17:26:00" "17:27:00" ...

使用了strptime函数

df33$Time <- strptime(df3$Time, format = "%H:%M:%S") 

这给出了附加的date/time

> str(df3$Time)
 POSIXlt[1:2075259], format: "2015-08-07 17:24:00" "2015-08-07 17:25:00" "2015-08-07 17:26:00" ...

只想提取时间而不更改 POSIXlt class。使用 strftime 函数

df3$Time <- strftime(df3$Time, format = "%H:%M:%S") 

但这会将 class 转换回 "char" -

> class(df3$Time)
[1] "character"

如何将 class 设置为 POSIX 或数字...

的时间提取出来

一个datetime对象包含日期和时间;您无法提取 'just time'。所以你必须想清楚你想要什么:

  • POSIXlt 是一种日期时间表示(作为组件列表)
  • POSIXct 是一种不同的日期时间表示(作为紧凑数字)

两者都没有省略日期部分。拥有有效对象后,您可以选择 仅显示 时间。但是你不能让日期部分从表示中消失。

如果您想要 POSIX 格式,唯一的方法是保持原样,每次显示时只提取 "time" 部分。但在内部,它始终是日期 + 时间。 但是,如果你想要它的数字形式,你可以简单地将它转换成数字。 例如,要获取时间作为自一天开始以来经过的秒数:

df3$Time=df3$Time$sec + df3$Time$min*60 + df3$Time$hour*3600

如果你的数据是

a <- "17:24:00"

b <- strptime(a, format = "%H:%M:%S")

您可以使用 lubridate 以获得 class integer

的结果
library(lubridate)
hour(b)
minute(b)

# > hour(b)
# [1] 17
# > minute(b)
# [1] 24


# > class(minute(b))
# [1] "integer"

您可以使用

将它们组合起来
# character
paste(hour(b),minute(b), sep=":")

# numeric
hour(b) + minute(b)/60

例如

如果您想对数据进行任何进一步操作,我不建议您这样做。但是,如果您想绘制结果,这样做可能会很方便。

您还可以使用 chron 包来提取一天中的时间:

library(chron) 

# current date/time in POSIXt format as an example
timenow <- Sys.time()

# create chron object "times"
onlytime <- times(strftime(timenow,"%H:%M:%S"))

> onlytime
[1] 14:18:00
> onlytime+1/24
[1] 15:18:00
> class(onlytime)
[1] "times"

这是我的习惯用法,用于仅从日期时间对象获取时间部分。我使用 lubridate 中的 floor_date() 来获取时间戳的午夜,并计算时间戳和当天午夜的差值。我在数据框中创建并存储了 lubridate(我相信)提供的 hms 对象,因为 class 具有易于阅读的 hh:mm:ss 格式,但基础值是秒的数值。这是我的代码:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

# Create timestamps
#
# Get timepart by subtacting the timestamp from it's floor'ed date, make sure
# you convert to seconds, and then cast to a time object provided by the
# `hms` package.
# See: https://www.rdocumentation.org/packages/hms/versions/0.4.2/topics/hms
dt <- tibble(dt=c("2019-02-15T13:15:00", "2019-02-19T01:10:33") %>% ymd_hms()) %>%
  mutate(timepart = hms::hms(as.numeric(dt - floor_date(dt, "1 day"), unit="secs")))

# Look at result
print(dt)
#> # A tibble: 2 x 2
#>   dt                  timepart
#>   <dttm>              <time>  
#> 1 2019-02-15 13:15:00 13:15   
#> 2 2019-02-19 01:10:33 01:10

# `hms` object is really a `difftime` object from documentation, but is made into a `hms`
# object that defaults to always store data in seconds.
dt %>% pluck("timepart") %>% str()
#>  'hms' num [1:2] 13:15:00 01:10:33
#>  - attr(*, "units")= chr "secs"

# Pull off just the timepart column
dt %>% pluck("timepart")
#> 13:15:00
#> 01:10:33

# Get numeric part.  From documentation, `hms` object always stores in seconds.
dt %>% pluck("timepart") %>% as.numeric()
#> [1] 47700  4233

reprex package (v0.2.1)

于 2019-02-15 创建

“现代”tidyverse 对此的回答是使用 hms::as_hms()

例如

library(tidyverse)
library(hms)

as_hms(1)
#> 00:00:01
as_hms("12:34:56")
#> 12:34:56

或者,使用您的示例数据:

x <- as.POSIXlt(c("17:24:00", "17:25:00", "17:26:00", "17:27:00"), format = "%H:%M:%S")

x
#>[1] "2021-04-10 17:24:00 EDT" "2021-04-10 17:25:00 EDT" "2021-04-10 17:26:00 EDT" "2021-04-10 17:27:00 EDT"

as_hms(x)
# 17:24:00
# 17:25:00
# 17:26:00
# 17:27:00

另请参阅此处的文档: https://hms.tidyverse.org/reference/hms.html