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

时间:2015-08-07 11:45:27

标签: r datetime strptime

想要将Time类更改为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") 

这会附加日期/时间

> 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类的情况下提取时间。使用strftime函数

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

但是这会将类转换回“char” -

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

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

提取时间

6 个答案:

答案 0 :(得分:15)

如果您的数据是

a <- "17:24:00"

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

您可以使用lubridate来获得课程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
例如

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

答案 1 :(得分:9)

date 时间对象包含日期和时间;你不能提取'只是时间'。所以你必须考虑你想要的东西:

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

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

答案 2 :(得分:3)

您还可以使用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"

答案 3 :(得分:2)

这是我习惯上从datetime对象获取时间部分的习惯用法。我使用lubridate中的floor_date()来获取时间戳的午夜,并获取时间戳与当天午夜的差。我创建并存储了hms所提供的lubridate对象(我相信)在数据帧中,因为该类具有易于理解的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创建

答案 4 :(得分:1)

如果你想要POSIX格式,唯一的办法就是保持原样,只提取&#34;时间&#34;每次显示它时的部分。但无论如何,内部始终是日期+时间。 但是,如果您想要数字,则只需将其转换为数字即可。 例如,要将时间作为从一天开始以来经过的秒数:

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

答案 5 :(得分:1)

对此的“现代”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

相关问题