R中的格式日期(年 - 月) [英] Format Date (Year-Month) in R

查看:1227
本文介绍了R中的格式日期(年 - 月)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将以下号码格式化为Year-Month
I条目,如下所示:

Is it possible to format the following number to Year-Month I entries as follows:

1402
1401
1312

1402 1401 1312

含义2014年1月2014年1月和2013年12月。

Meaning February 2014. January 2014 and December 2013.

我尝试过:

date <- 1402
date <- as.Date(as.character(date), format = "%y%m")

但是我得到一个NA作为输出。

But I get an NA as an output.

推荐答案

动物园包中有一个直接处理年/月对象的yearmon类:

The zoo package has a "yearmon" class that directly handles year/month objects:

library(zoo)
nums <- c(1402, 1401, 1312)
ym <- as.yearmon(as.character(nums), "%y%m")

给:

> ym
[1] "Feb 2014" "Jan 2014" "Dec 2013"

这篇关于R中的格式日期(年 - 月)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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