简体   繁体   English

以 yymm 格式将月份添加到日期

[英]Add months to a date in format yymm

I am trying to add a specified number of months to a yymm formatted date.我正在尝试将指定的月数添加到yymm格式的日期。 I'm having a hard time dealing with cases when mm exceeds 12. So, for example:我很难处理 mm 超过 12 的情况。例如:

9109 (ie. 1991-Sept) + 17 months should yield 9302. 9109(即 1991 年至 9 月)+ 17 个月应产生 9302。

9103 + 3 months should yield 9106 9103 + 3 个月应该会产生 9106

Currently, I am trying to use modular arithmetic to obtain the additional months in yymm format, but this isn't working as well as hoped.目前,我正在尝试使用模运算来获得 yymm 格式的额外月份,但这并不像希望的那样工作。 I convert 17 months into 0105, but adding 0105 to 9109 leads to 9214, which is nonsensical.我将 17 个月转换为 0105,但将 0105 添加到 9109 会导致 9214,这是无意义的。

Thank you for any help.感谢您的任何帮助。

Here are several approaches.这里有几种方法。 All also work if x is a vector.如果 x 是一个向量,所有这些也都有效。

1) as.yearmon Suggest storing your year/month data as yearmon objects. 1) as.yearmon建议将年/月数据存储为 yearmon 对象。 They are stored internally as year + fraction where fraction is 0 for January, 1/12 for February, ..., 11/12 for December and when printed show in a recognizable form.它们在内部存储为年 + 分数,其中分数为 0 表示 1 月,1/12 表示 2 月,...,11/12 表示 12 月,并且当以可识别的形式打印显示时。

Note that because x is numeric if we are in 2000 or 2001 then x does not have 4 digits so we need to zero fill as shown should such dates be in the input.请注意,如果我们在 2000 年或 2001 年,x 是数字,那么 x 没有 4 位数字,因此如果输入中有这样的日期,我们需要填零,如图所示。

It's probably easier to just keep everything as yearmon objects but you could replace the last line with as.numeric(format(ym + 17/12, "%y%m")) if you want the output in the same form as x .将所有内容保留为 yearmon 对象可能更容易,但如果您希望 output 的格式与x相同,则可以用as.numeric(format(ym + 17/12, "%y%m"))替换最后一行。

library(zoo)

x <- 9109 
ym <- as.yearmon(sprintf("%04f", x), "%y%m")
ym + 17/12
## [1] "Feb 1993"

2) as.POSIXlt Another approach is to convert to POSIXlt and then add 17 to the months. 2) as.POSIXlt另一种方法是转换为 POSIXlt 然后将 17 添加到月份。 This does not use any packages.这不使用任何包。 We have returned a character string below but you can convert it to numeric using as.numeric if you want it in exactly the same form as x .我们在下面返回了一个字符串,但是如果您希望它的格式与x完全相同,您可以使用as.numeric将其转换为数字。

lt <- as.POSIXlt(sprintf("%04d01", x), format = "%y%m%d")
lt$mon <- lt$mon + 17
format(lt, "%y%m")
## [1] "9302"

3) modular arithmetic To use modular arithmetic convert it to months, add 17 and convert back. 3) 模运算要使用模运算将其转换为月,加 17 并转换回来。 This does not use any packages.这不使用任何包。

months <- (12 * x %/% 100) + (x %% 100) - 1 + 17
100 * (months %/% 12) + months %% 12 + 1
## [1] 9302

Using seq.Date , only base R needed.使用seq.Date ,只需要基础 R 。

We paste a phantom day 1 to the dates and convert as.Date , generate a seq uence of length m months, and convert the formatted result into as.numeric .我们将虚拟的第1paste到日期并转换为as.Date ,生成长度为 m 个月的seq ,并将格式化的结果转换为as.numeric

add_months <- function(x, m) {
  as.numeric(
    strftime(seq.Date(as.Date(paste0(9109, 1), "%y%m%d"),
                      by=paste(m, "months"), length.out=2)[2],
             "%y%m")
  )
}

add_months(9109, 17)
# [1] 9302

add_months(9103, 3)
# [1] 9106

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

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