减去年月格式的时间 [英] Subtract months from time in format year-month

查看:42
本文介绍了减去年月格式的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以年和月的格式从给定的日期中减去月.

I want to subtract months from given date in year and month format.

global_date = "2017-01"

我正在使用 zoo 库进行如下转换:

I am converting it with the zoo library as follows:

as.yearmon(global_date) - 0.1

但这给了我2016年11月,我希望它是'201612'

but it gives me Nov 2016, I want it as '201612'

我如何在R中做到这一点?

How can I do it in R?

推荐答案

要减去一个月,我们应该减去 1/12 ,即 0.083 ,而不是 0.1

As we want to subtract one month, we should subtract 1/12 which is 0.083 and not 0.1

library(zoo)
as.yearmon(global_date) - (1/12)
#[1] "Dec 2016"

如果我们需要以上述格式输出

If we need output in the mentioned format

format(as.yearmon(global_date) - (1/12), "%Y%m")
#[1] "201612"

这篇关于减去年月格式的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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