R将日期格式从字符mmm dd,yyyy格式化为上课日期 [英] R formatting a date from a character mmm dd, yyyy to class date

查看:64
本文介绍了R将日期格式从字符mmm dd,yyyy格式化为上课日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mmm dd,yyyy格式的字符格式化日期

I am trying to format a date from a character of form mmm dd, yyyy

我尝试过:

date1 <- "Dec 05, 2016"
date2 <- format(date1, format="%d %B %Y")
 class(date2)
 date2

但是这不起作用.谢谢您的帮助.

but this does not work. Thank you for any help.

然后我尝试按照建议进行操作:

I then tried as suggested:

date1 <- "Dec 05, 2016"
date2 <- format(as.Date(date1, "%b %d, %Y"), "%d %B %Y")
date2  <- as.Date(date2)

class(date2)
date2

但这仍然没有隐瞒上课日期,但是是上课日期:character

but this still did not covert to the class date but was class: character

这似乎有效ref:将字符转换为R中的日期

library(lubridate)
date1 <- "Dec 05, 2016"
date2 <- mdy(date1)
class(date2)
date2

推荐答案

我们可以转换为'Date'类,然后转换为感兴趣的 format 格式

We can convert to 'Date' class and then convert to the format of interest

format(as.Date(date1, "%b %d, %Y"), "%d %B %Y")
#[1] "05 December 2016"

这篇关于R将日期格式从字符mmm dd,yyyy格式化为上课日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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