4

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

cluna
  • 85
  • 2
  • 8

2 Answers2

7

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
austensen
  • 2,857
  • 13
  • 24
0

I once had a problem where the date-format was like: "2019-12-06T09:27:14".

For me ymd_hms did not work and I did not find a fitting parser, so the solution was first to string_replace the uneccessary part and then to parse to date as usual.. (and discard the original column if needed)

 your_df %>%mutate(date_published = 
                   str_replace(article.date_published,T\\d\\d\\:\\d\\d\\:\\d\\d', ''), 
                   date_published= ymd(date_published))%>%
           select(-contains('article.date'))
Aku
  • 660
  • 6
  • 9