将yyyymm格式转换为R中的日期? [英] Convert a yyyymm format to date in R?

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

问题描述

年和月表示为201603,代表2016年3月.如何将此数值转换为日期格式,例如R中的"2016年3月"还是"2016-03"?

The year and month is represented as 201603 representing March,2016. How to convert this numerical value into date format like "March-2016" or "2016-03" in R?

推荐答案

日期应始终具有给定的日期,因此请考虑将日期添加到 YYYYMM 的末尾(在下面的示例中, 01 每月的第一天).将数字值转换为字符,并将 character 转换为 Date :

A date should always have a given day, therefore consider to add the day at the end of YYYYMM (in the example below 01 the first day of the month). Convert the numeric value to character and the character to a Date:

as.Date(paste0(as.character(201603),'01'),format ='%Y%m%d')

这篇关于将yyyymm格式转换为R中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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