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

查看:13
本文介绍了在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天全站免登陆