Change Date Format from %y-%m-%d %h:%m:%s to %Y%M%D with lubridate and mutate

我怕爱的太早我们不能终老 提交于 2021-01-03 03:56:12

问题


I've got a tbl_dfwith two columns StartTime and StopTime. Both are dttm.

I want to change its format from "%y-%m-%d %h:%m:%s" to "%y%m%d".

I've tried

data <- mutate(data, StartTime = ymd(StartTime), StopTime = ymd(StopTime))

But it returns

Warning messages: 1: All formats failed to parse. No formats found. 2: All formats failed to parse. No formats found.

How can I do it?

Please, don't send other questions that don't use lubridate package.

Thanks


回答1:


I think this should work

library(dplyr)
library(lubridate)


df <- data_frame(StartTime1 = "2017-04-28 12:50:45")

df %>% mutate(StartTime2 = ymd_hms(StartTime1),
              StartTime3 = as_date(StartTime2))

#> # A tibble: 1 x 3
#>            StartTime1          StartTime2 StartTime3
#>                 <chr>              <dttm>     <date>
#> 1 2017-04-28 12:50:45 2017-04-28 12:50:45 2017-04-28


来源:https://stackoverflow.com/questions/44312872/change-date-format-from-y-m-d-hms-to-ymd-with-lubridate-and-mutate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!