将日期和时间转换为类别

时间:2019-09-25 18:50:10

标签: r

我有一个数据框df,如下所示。有日期和时间格式。有没有一种方法可以将它们转换为年,月,日,日期,小时,分钟和秒。例如

      df <- 
      Date                    A     B     C
    1 2017-09-19 00:00:00    42    27    39
    2 2017-09-19 00:05:00    40    36    42
    3 2017-09-19 00:10:00    41    40    33
    4 2017-09-19 00:15:00    45    31    26
    5 2017-09-19 00:20:00    25    44    29
    6 2017-09-19 00:25:00    39    34    42
    7 2017-09-19 00:30:00    44    39    24
    8 2017-09-19 00:35:00    25    44    34
    9 2017-09-19 00:40:00    39    41    35

我需要在下面

      df <- 
      Date                    A     B     C    Year     Month    Day  Date   Hour   Min   Sec
    1 2017-09-19 00:00:00    42    27    39    2017      Sep     Tue   19     00    00     00
    2 2017-09-19 00:05:00    40    36    42    2017      Sep     Tue   19     00    05     00
    3 2017-09-19 00:10:00    41    40    33    2017      Sep     Tue   19     00    10     00
    4 2017-09-19 00:15:00    45    31    26    2017      Sep     Tue   19     00    15     00
    5 2017-09-19 00:20:00    25    44    29    2017      Sep     Tue   19     00    20     00
    6 2017-09-19 00:25:00    39    34    42    2017      Sep     Tue   19     00    25     00
    7 2017-09-19 00:30:00    44    39    24    2017      Sep     Tue   19     00    30     00
    8 2017-09-19 00:35:00    25    44    34    2017      Sep     Tue   19     00    35     00
    9 2017-09-19 01:40:00    39    41    35    2017      Sep     Tue   19     01    40     00

1 个答案:

答案 0 :(得分:2)

这是lubridate的一个选项。使用ymd_hms中的lubridate将'Date'转换为DateTime类,并使用format或直接使用适当的函数提取组件

library(lubridate)
library(dplyr)
df %>%
  mutate(Date = ymd_hms(Date),
         Year = year(Date),
         Month = format(Date, "%b"),
         Day = format(Date, "%a"),
         DateDay = day(Date),
         Hour = hour(Date),
         Min =  minute(Date),
         Sec = second(Date))
#                 Date  A  B  C Year Month Day DateDay Hour Min Sec
#1 2017-09-19 00:00:00 42 27 39 2017   Sep Tue      19    0   0   0
#2 2017-09-19 00:05:00 40 36 42 2017   Sep Tue      19    0   5   0
#3 2017-09-19 00:10:00 41 40 33 2017   Sep Tue      19    0  10   0
#4 2017-09-19 00:15:00 45 31 26 2017   Sep Tue      19    0  15   0
#5 2017-09-19 00:20:00 25 44 29 2017   Sep Tue      19    0  20   0
#6 2017-09-19 00:25:00 39 34 42 2017   Sep Tue      19    0  25   0
#7 2017-09-19 00:30:00 44 39 24 2017   Sep Tue      19    0  30   0
#8 2017-09-19 00:35:00 25 44 34 2017   Sep Tue      19    0  35   0
#9 2017-09-19 00:40:00 39 41 35 2017   Sep Tue      19    0  40   0

请注意,如果我们需要两位数字的“小时”,“分钟”,“秒”,请使用format,即

df %>%
   mutate(Date = ymd_hms(Date),
             Year = year(Date),
          Month = format(Date, "%b"),
          Day = format(Date, "%a"),
          DateDay = day(Date),
          Hour = format(Date, "%H"),
          Min =  format(Date, "%M"),
          Sec =  format(Date, "%S"))
                 Date  A  B  C Year Month Day DateDay Hour Min Sec
#1 2017-09-19 00:00:00 42 27 39 2017   Sep Tue      19   00  00  00
#2 2017-09-19 00:05:00 40 36 42 2017   Sep Tue      19   00  05  00
#3 2017-09-19 00:10:00 41 40 33 2017   Sep Tue      19   00  10  00
#4 2017-09-19 00:15:00 45 31 26 2017   Sep Tue      19   00  15  00
#5 2017-09-19 00:20:00 25 44 29 2017   Sep Tue      19   00  20  00
#6 2017-09-19 00:25:00 39 34 42 2017   Sep Tue      19   00  25  00
#7 2017-09-19 00:30:00 44 39 24 2017   Sep Tue      19   00  30  00
#8 2017-09-19 00:35:00 25 44 34 2017   Sep Tue      19   00  35  00
#9 2017-09-19 00:40:00 39 41 35 2017   Sep Tue      19   00  40  00