问题
I've got a tbl_df
with 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