将时间戳转换为日期、时间、小时、分钟

Convert Time Stamp into date, time, hour, minute

我的数据集中有一列如下:

df$timestamp

timestamp
2018-01-17 10:35:00 UTC
2015-05-08 17:30:00 UTC
2017-11-22 07:15:00 UTC
2017-12-05 07:30:00 UTC

str(timestamp)
chr [1:1196940] "2018-01-17 10:35:00 UTC" "2015-05-08 17:30:00 UTC" "2017-11-22 07:15:00 UTC" "2017-12-05 07:30:00 UTC" ...

我希望能够分别提取年月日和时间。

我的最终输出应该是这样的:

timestamp                           date          time     Hour      Minute
2018-01-17 10:35:00 UTC       2018-01-17      10:35:00       10          35
2015-05-08 17:30:00 UTC       2015-05-08      17:30:00       17          30      
2017-11-22 07:15:00 UTC       2017-11-22      07:15:00       07          15
2017-12-05 07:30:00 UTC       2017-12-05      07:30:00       07          30

有没有一种简单的方法可以使用像 lubridate 这样的包来做到这一点,或者我是否需要通过 10 个字符解析数据,然后是 space,等等?

您可以使用 lubridate 中的访问器来创建小时、分钟和秒列。我不清楚你想要 "time" 列的格式。R 支持日期时间和日期,但不仅仅是时间。 lubridate有三个"time length"类、perioddurationinterval。我在这里选择了 duration,它跟踪物理时间而不是时钟时间,但您可以根据需要进行更改。

library(tidyverse)
library(lubridate)
tbl <- tibble(
  timestamp = c(
    "2018-01-17 10:35:00 UTC",
    "2015-05-08 17:30:00 UTC",
    "2017-11-22 07:15:00 UTC",
    "2017-12-05 07:30:00 UTC"
  )
)

tbl %>%
  mutate(
    timestamp = ymd_hms(timestamp),
    date = date(timestamp),
    hours = hour(timestamp),
    minutes = minute(timestamp),
    seconds = second(timestamp),
    time = pmap(
      .l = list(hours, minutes, seconds),
      .f = ~ dhours(..1) + dminutes(..2) + dseconds(..3)
    )
  )
#> # A tibble: 4 x 6
#>   timestamp           date       hours minutes seconds time          
#>   <dttm>              <date>     <int>   <int>   <dbl> <list>        
#> 1 2018-01-17 10:35:00 2018-01-17    10      35       0 <S4: Duration>
#> 2 2015-05-08 17:30:00 2015-05-08    17      30       0 <S4: Duration>
#> 3 2017-11-22 07:15:00 2017-11-22     7      15       0 <S4: Duration>
#> 4 2017-12-05 07:30:00 2017-12-05     7      30       0 <S4: Duration>

reprex package (v0.2.0) 创建于 2018-07-23。

这是一个使用 purrr::map_df

tidyverse 选项
library(tidyverse)
bind_cols(df, map_df(
    c(date = "%Y/%m/%d", time = "%H:%M:%S", Hour = "%H", Minute = "%M"),
    ~format(as.POSIXct(df$timestamp), format = .x)))
#                timestamp       date     time Hour Minute
#1 2018-01-17 10:35:00 UTC 2018/01/17 10:35:00   10     35
#2 2015-05-08 17:30:00 UTC 2015/05/08 17:30:00   17     30
#3 2017-11-22 07:15:00 UTC 2017/11/22 07:15:00   07     15
#4 2017-12-05 07:30:00 UTC 2017/12/05 07:30:00   07     30

示例数据

df <- read.table(text =
    "timestamp
'2018-01-17 10:35:00 UTC'
'2015-05-08 17:30:00 UTC'
'2017-11-22 07:15:00 UTC'
'2017-12-05 07:30:00 UTC'", header = T)

类似于 Calum 你的回答,但我想我们可以直接使用日期、小时和分钟。

library(lubridate)
new <- df %>%
  mutate(date = date(ymd_hms(test$timestamp)), 
  time = format(ymd_hms(test$timestamp), format = c("%H:%M:%S")), 
  Hour = hour(ymd_hms(test$timestamp)),
  Minute = minute(ymd_hms(test$timestamp)))