R:从数字日期提取年和月 [英] R: Extracting year and month from a numeric date

查看:966
本文介绍了R:从数字日期提取年和月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数字模式中有日期,如下所示:
20/05/2012

I have dates in Numeric mode like this: 20/05/2012

从这里提取年份和月份最优雅的方式是什么?

What's the most elegant way to extract the year and month from this?

推荐答案

首先您需要将其转换为日期对象:

First you need to convert it to a date object:

x <- as.Date("20/05/2012", format="%d/%m/%Y")

然后获取月份号码:

format(x,"%m")

并获取年份号码:

format(x,"%Y")

如果你想要月份名称(注意,这是你的机器语言):

If you want the month name (note, this is in your machine language):

months(x)

这篇关于R:从数字日期提取年和月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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