Dev P
Dev P

Reputation: 449

Convert Date and time into categories

I have a dataframe df as shown below. It is there is Date and time format. Is there a way to convert them into Year, Month, Day, Date, Hour, Minute and Second. For example

      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

I need below

      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

Upvotes: 0

Views: 253

Answers (1)

akrun
akrun

Reputation: 887501

Here is one option with lubridate. Convert the 'Date' to DateTime class with ymd_hms from lubridate and extract the components either with format or directly with the appropriate function

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

Note that if we need two digit 'Hour', 'Min', 'Sec', use the format i.e.

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

Upvotes: 2

Related Questions