使用 lubridate 和 mutate 将日期格式从 %y-%m-%d %h:%m:%s 更改为 %Y%M%D [英] Change Date Format from %y-%m-%d %h:%m:%s to %Y%M%D with lubridate and mutate

查看:101
本文介绍了使用 lubridate 和 mutate 将日期格式从 %y-%m-%d %h:%m:%s 更改为 %Y%M%D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tbl_df 有两列 StartTimeStopTime.两者都是 dttm.

我想将其格式从 "%y-%m-%d %h:%m:%s" 更改为 "%y%m%d".

我试过了

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

但它回来了

<块引用><块引用>

警告消息:1:解析所有格式失败.没有找到格式.2:所有格式都无法解析.没有找到格式.

我该怎么做?

请不要发送其他不使用 lubridate 包的问题.

谢谢

解决方案

我认为这应该可行

library(dplyr)图书馆(润滑)df <- data_frame(StartTime1 = "2017-04-28 12:50:45")df %>% mutate(StartTime2 = ymd_hms(StartTime1),StartTime3 = as_date(StartTime2))#># 小费:1 x 3#>开始时间 1 开始时间 2 开始时间 3#><chr><dttm><日期>#>1 2017-04-28 12:50:45 2017-04-28 12:50:45 2017-04-28

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

这篇关于使用 lubridate 和 mutate 将日期格式从 %y-%m-%d %h:%m:%s 更改为 %Y%M%D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆