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

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

问题描述

是否可以将以下数字格式化为年月我的条目如下:

14021401第1312章

表示 2014 年 2 月、2014 年 1 月和 2013 年 12 月.

我试过了:

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

但我得到一个 NA 作为输出.

解决方案

zoo包有一个"yearmon"类直接处理年/月对象:

图书馆(动物园)nums <- c(1402, 1401, 1312)ym <- as.yearmon(as.character(nums), "%y%m")

给予:

<代码>>嗯[1] 2014 年 2 月" 2014 年 1 月" 2013 年 12 月"

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

1402 1401 1312

Meaning February 2014. January 2014 and December 2013.

I tried:

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

But I get an NA as an output.

解决方案

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")

giving:

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

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

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