简体   繁体   English

如何从年/月数值变量 (R) 格式化月变量

[英]How to format monthly variable from year/month numeric variable (R)

I have a vector with numeric (not formated) year/month variable.我有一个带有数字(未格式化)年/月变量的向量。 Consider the vector below for all months of year 2012. How could I create a formated monthly variable from it?考虑下面 2012 年所有月份的向量。如何从中创建格式化的月度变量?

ym <- c(201201,201202,201203,201204,201205,201206,201207,201208,201209,201210,201211,201212)

May be need可能需要

library(zoo)
format(as.yearmon(as.character(ym), '%Y%m'), '%b')

Or in base R或在base R

format(as.Date(paste0(ym, '01'), '%Y%m%d'), '%b')

We can use %% , eg,我们可以使用%% ,例如,

> month.abb[ym %% 100]
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM