将日期转换为R中特定格式的字符 [英] Convert date to character in particular format In R

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

问题描述

我需要映射3-4个具有不同日期格式的不同数据框. 如何将日期转换为以下格式:

I need to map 3-4 different dataframes that have different date formats. How can we convert date in format:

YYYY-MM-DD

以以下格式的字符:

MMM-YY

推荐答案

从字符串创建日期对象(如果您的列已经采用日期格式,请跳过此步骤):

Create a date object from the string (skip this if your column is already in the Date format):

original_date <- as.Date("2017-07-19", "%Y-%m-%d")

然后将原始日期格式化为新格式:

Then format the original date to the new format:

format(original_date, "%b-%y")   
# "Jul-17"

%b表示月份的3个字母的缩写,%y是不带世纪的年份.您可以在这里找到更多这些代码及其含义: http://strftime.org/

The %b indicates the 3-letter abbreviation of the month and %y is the year without the century. You can find more of these codes and their meaning here: http://strftime.org/

这篇关于将日期转换为R中特定格式的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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