且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 lubridate 和 mutate 将日期格式从 %y-%m-%d %h:%m:%s 更改为 %Y%M%D

更新时间:2022-10-28 12:42:57

我认为这应该可行

library(dplyr)图书馆(润滑)df 

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

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