如何分隔日期和时间示例:R中的12/25/17 0:00?

时间:2019-04-16 19:57:50

标签: r datetime date-formatting

如果the_date列如下,如何将日期和时间分成2个不同的变量:

data set image for reference

the_date
12/25/17 0:00

如何分别检索

  • 年份为2017,
  • 月份为12或12月,
  • 日期为12,并且
  • 时间为0.00?

1 个答案:

答案 0 :(得分:1)

转换为DateTime之后,我们提取每个组件

library(lubridate)
library(dplyr)
df1 %>%
    mutate(v1 = mdy_hm(v1), 
           Year = year(v1),
           Month = month(v1), 
           Date = day(v1), 
           time = format(v1, "%H:%M:%S"))
# A tibble: 1 x 5
#   v1                   Year Month  Date time    
#   <dttm>              <dbl> <dbl> <int> <chr>   
#1 2017-12-25 00:00:00  2017    12    25 00:00:00

数据

df1 <- tibble(v1 = "12/25/17 0:00")