简体   繁体   English

字符串序列以一个运行字符开始,然后是一个数字向量

[英]String sequence starting with a running character and then with a vector of numbers

I want to have the following string sequence as result:我想得到以下字符串序列作为结果:

"2016-01" "2016-02" "2016-03" "2016-04" "2016-05" "2016-06" "2016-07" "2016-08" "2016-09" "2016-10" "2016-11" "2016-12" "2017-01" "2017-02" "2017-03" "2017-04" "2017-05" "2017-06" "2017-07" "2017-08" "2017-09" "2017-10" "2017-11" "2017-12" "2018-01" "2018-02" "2018-03" "2018-04" "2018-05" "2018-06" "2018-07" "2018-08" "2018-09" "2018-10" "2018-11" "2018-12" "2019-01" "2019-02" "2019-03" "2019-04" "2019-05" "2019-06" "2019-07" "2019-08" "2019-09" "2019-10" "2019-11" "2019-12" "2020-01" "2020-02" "2020-03" "2020-04" "2020-05" "2020-06" "2020-07" "2020-08" "2020-09" "2020-10" "2020-11" "2020-12" "2016-01" "2016-02" "2016-03" "2016-04" "2016-05" "2016-06" "2016-07" "2016-08" "2016-09" "2016-10" "2016-11" "2016-12" "2017-01" "2017-02" "2017-03" "2017-04" "2017-05" "2017-06" "2017-07" "2017-08" "2017-09" "2017-10" "2017-11" "2017-12" "2018-01" "2018-02" "2018-03" "2018-04" "2018-05" "2018-06" "2018-07" "2018-08" "2018-09" "2018-10" "2018-11" "2018-12" "2019-01" "2019-02" "2019-03" "2019-04" "2019-05" "2019-06" "2019-07" "2019-08" "2019-09" "2019-10" "2019-11" "2019-12" "2020-01" "2020-02" "2020-03" "2020-04" "2020-05" "2020-06" "2020-07" "2020-08" "2020-09" "2020-10" "2020-11" "2020-12"

and I wrote the following code:我写了以下代码:

c(sprintf("2016-%02d", 1:12), 
  sprintf("2017-%02d", 1:12),
  sprintf("2018-%02d", 1:12),
  sprintf("2019-%02d", 1:12),
  sprintf("2020-%02d", 1:12))

Is there any other shorter and eleganter solution?还有其他更短更优雅的解决方案吗? For example any suggestion to combine the first part of sprintf ?例如,任何建议结合sprintf的第一部分?

1) This will give the indicated character vector or just use ym to get yearmon class vector which may be more convenient as it is represented internally as year + fraction where fraction is 0 for Jan, 1/12 for Feb, ..., 11/12 for Dec and so may be manipulated in relevant ways such as using it as the X axis for a plot. 1)这将给出指示的字符向量或仅使用 ym 来获取 yearmon 类向量,这可能更方便,因为它在内部表示为 year + fraction 其中分数为 0 表示 1 月,1/12 表示 2 月,...,11 /12 表示 Dec,因此可以通过相关方式进行操作,例如将其用作绘图的 X 轴。

library(zoo)
ym <- seq(as.yearmon("2016-01"),  as.yearmon("2020-12"), 1/12)
format(ym, "%Y-%m")

An alternate way of forming ym is:形成 ym 的另一种方法是:

ym <- ts(start = 2006, end = c(2012, 12), freq = 12) |> 
  time() |>
  as.yearmon()

2) This would also work using only base R. You might prefer to just use d as it could be used in plotting as well. 2)这也适用于仅使用基础 R。您可能更喜欢使用 d,因为它也可以用于绘图。

d <- seq(as.Date("2016-01-01"), as.Date("2020-12-01"), by = "month")
format(d, "%Y-%m")

3) This base solution is shorter but lacks the flexibility of using Date or yearmon classes. 3)这个基本解决方案更短,但缺乏使用 Date 或 yearmon 类的灵活性。

sprintf("%d-%02d", rep(2006:2012, each = 12), 1:12)

With base R, try:使用基础 R,尝试:

seq.Date(from = as.Date("2016-01-01"), to = as.Date("2020-12-01"), by = "1 month") |> format("%Y-%m")

 [1] "2016-01" "2016-02" "2016-03" "2016-04" "2016-05" "2016-06" "2016-07"
 [8] "2016-08" "2016-09" "2016-10" "2016-11" "2016-12" "2017-01" "2017-02"
[15] "2017-03" "2017-04" "2017-05" "2017-06" "2017-07" "2017-08" "2017-09"
[22] "2017-10" "2017-11" "2017-12" "2018-01" "2018-02" "2018-03" "2018-04"
[29] "2018-05" "2018-06" "2018-07" "2018-08" "2018-09" "2018-10" "2018-11"
[36] "2018-12" "2019-01" "2019-02" "2019-03" "2019-04" "2019-05" "2019-06"
[43] "2019-07" "2019-08" "2019-09" "2019-10" "2019-11" "2019-12" "2020-01"
[50] "2020-02" "2020-03" "2020-04" "2020-05" "2020-06" "2020-07" "2020-08"
[57] "2020-09" "2020-10" "2020-11" "2020-12"

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

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