将“yyyy-mm-dd”转换为月-年格式,并在r中生成序列

v2g6jxz6  于 2023-03-27  发布在  其他
关注(0)|答案(4)|浏览(126)

我想创建一个从2010-04-01到2040-03-01的日期序列,并将日期格式表示为“April-2010”或“Apr 2010”。然而,当我使用as.yearmon()时,它只返回“4 2010”而不是“Apr 2010”。
这是我的第一个代码,我试图将“2010-04-01”转换为“Apr 2010”的格式,但代码的结果是“4 2010”。

as.yearmon("2010-04-01", format = "%Y-%m-%d")

此外,我试图制作一个序列,但发生了一个错误(错误在del/by:二元运算符的非数值参数)

seq(as.yearmon("2010-04-01"), as.yearmon("2040-03-01"), by = "1 month")

总而言之,我想做一个序列如下。你能帮助我怎么做吗?

"Apr 2010", "May 2010", "Jun 2010", ... , "Mar 2040"
xxhby3vn

xxhby3vn1#

我们可以使用format

# example dates
dates <- seq(as.Date("2023-03-01"), as.Date("2023-12-01"), by = "month")

format(dates, "%b %Y")
# or
format(dates, "%B-%Y")
[1] "Mrz 2023" "Apr 2023" "Mai 2023" "Jun 2023" "Jul 2023" "Aug 2023" "Sep 2023" "Okt 2023" "Nov 2023" "Dez 2023"

 [1] "März-2023"      "April-2023"     "Mai-2023"       "Juni-2023"      "Juli-2023"      "August-2023"    "September-2023" "Oktober-2023"  
 [9] "November-2023"  "Dezember-2023"
wz3gfoph

wz3gfoph2#

您可以先使用as.Date将开始和结束日期转换为日期格式,然后使用as.yearmon将序列转换为所需的格式,如下所示:

library(zoo)
as.yearmon(seq(as.Date("2010-04-01"), as.Date("2040-03-01"), by = "1 month"))
#>   [1] "Apr 2010" "May 2010" "Jun 2010" "Jul 2010" "Aug 2010" "Sep 2010"
#>   [7] "Oct 2010" "Nov 2010" "Dec 2010" "Jan 2011" "Feb 2011" "Mar 2011"
#>  [13] "Apr 2011" "May 2011" "Jun 2011" "Jul 2011" "Aug 2011" "Sep 2011"
#>  [19] "Oct 2011" "Nov 2011" "Dec 2011" "Jan 2012" "Feb 2012" "Mar 2012"
#>  [25] "Apr 2012" "May 2012" "Jun 2012" "Jul 2012" "Aug 2012" "Sep 2012"
#>  [31] "Oct 2012" "Nov 2012" "Dec 2012" "Jan 2013" "Feb 2013" "Mar 2013"
#>  [37] "Apr 2013" "May 2013" "Jun 2013" "Jul 2013" "Aug 2013" "Sep 2013"
#>  [43] "Oct 2013" "Nov 2013" "Dec 2013" "Jan 2014" "Feb 2014" "Mar 2014"
#>  [49] "Apr 2014" "May 2014" "Jun 2014" "Jul 2014" "Aug 2014" "Sep 2014"
#>  [55] "Oct 2014" "Nov 2014" "Dec 2014" "Jan 2015" "Feb 2015" "Mar 2015"
#>  [61] "Apr 2015" "May 2015" "Jun 2015" "Jul 2015" "Aug 2015" "Sep 2015"
#>  [67] "Oct 2015" "Nov 2015" "Dec 2015" "Jan 2016" "Feb 2016" "Mar 2016"
#>  [73] "Apr 2016" "May 2016" "Jun 2016" "Jul 2016" "Aug 2016" "Sep 2016"
#>  [79] "Oct 2016" "Nov 2016" "Dec 2016" "Jan 2017" "Feb 2017" "Mar 2017"
#>  [85] "Apr 2017" "May 2017" "Jun 2017" "Jul 2017" "Aug 2017" "Sep 2017"
#>  [91] "Oct 2017" "Nov 2017" "Dec 2017" "Jan 2018" "Feb 2018" "Mar 2018"
#>  [97] "Apr 2018" "May 2018" "Jun 2018" "Jul 2018" "Aug 2018" "Sep 2018"
#> [103] "Oct 2018" "Nov 2018" "Dec 2018" "Jan 2019" "Feb 2019" "Mar 2019"
#> [109] "Apr 2019" "May 2019" "Jun 2019" "Jul 2019" "Aug 2019" "Sep 2019"
#> [115] "Oct 2019" "Nov 2019" "Dec 2019" "Jan 2020" "Feb 2020" "Mar 2020"
#> [121] "Apr 2020" "May 2020" "Jun 2020" "Jul 2020" "Aug 2020" "Sep 2020"
#> [127] "Oct 2020" "Nov 2020" "Dec 2020" "Jan 2021" "Feb 2021" "Mar 2021"
#> [133] "Apr 2021" "May 2021" "Jun 2021" "Jul 2021" "Aug 2021" "Sep 2021"
#> [139] "Oct 2021" "Nov 2021" "Dec 2021" "Jan 2022" "Feb 2022" "Mar 2022"
#> [145] "Apr 2022" "May 2022" "Jun 2022" "Jul 2022" "Aug 2022" "Sep 2022"
#> [151] "Oct 2022" "Nov 2022" "Dec 2022" "Jan 2023" "Feb 2023" "Mar 2023"
#> [157] "Apr 2023" "May 2023" "Jun 2023" "Jul 2023" "Aug 2023" "Sep 2023"
#> [163] "Oct 2023" "Nov 2023" "Dec 2023" "Jan 2024" "Feb 2024" "Mar 2024"
#> [169] "Apr 2024" "May 2024" "Jun 2024" "Jul 2024" "Aug 2024" "Sep 2024"
#> [175] "Oct 2024" "Nov 2024" "Dec 2024" "Jan 2025" "Feb 2025" "Mar 2025"
#> [181] "Apr 2025" "May 2025" "Jun 2025" "Jul 2025" "Aug 2025" "Sep 2025"
#> [187] "Oct 2025" "Nov 2025" "Dec 2025" "Jan 2026" "Feb 2026" "Mar 2026"
#> [193] "Apr 2026" "May 2026" "Jun 2026" "Jul 2026" "Aug 2026" "Sep 2026"
#> [199] "Oct 2026" "Nov 2026" "Dec 2026" "Jan 2027" "Feb 2027" "Mar 2027"
#> [205] "Apr 2027" "May 2027" "Jun 2027" "Jul 2027" "Aug 2027" "Sep 2027"
#> [211] "Oct 2027" "Nov 2027" "Dec 2027" "Jan 2028" "Feb 2028" "Mar 2028"
#> [217] "Apr 2028" "May 2028" "Jun 2028" "Jul 2028" "Aug 2028" "Sep 2028"
#> [223] "Oct 2028" "Nov 2028" "Dec 2028" "Jan 2029" "Feb 2029" "Mar 2029"
#> [229] "Apr 2029" "May 2029" "Jun 2029" "Jul 2029" "Aug 2029" "Sep 2029"
#> [235] "Oct 2029" "Nov 2029" "Dec 2029" "Jan 2030" "Feb 2030" "Mar 2030"
#> [241] "Apr 2030" "May 2030" "Jun 2030" "Jul 2030" "Aug 2030" "Sep 2030"
#> [247] "Oct 2030" "Nov 2030" "Dec 2030" "Jan 2031" "Feb 2031" "Mar 2031"
#> [253] "Apr 2031" "May 2031" "Jun 2031" "Jul 2031" "Aug 2031" "Sep 2031"
#> [259] "Oct 2031" "Nov 2031" "Dec 2031" "Jan 2032" "Feb 2032" "Mar 2032"
#> [265] "Apr 2032" "May 2032" "Jun 2032" "Jul 2032" "Aug 2032" "Sep 2032"
#> [271] "Oct 2032" "Nov 2032" "Dec 2032" "Jan 2033" "Feb 2033" "Mar 2033"
#> [277] "Apr 2033" "May 2033" "Jun 2033" "Jul 2033" "Aug 2033" "Sep 2033"
#> [283] "Oct 2033" "Nov 2033" "Dec 2033" "Jan 2034" "Feb 2034" "Mar 2034"
#> [289] "Apr 2034" "May 2034" "Jun 2034" "Jul 2034" "Aug 2034" "Sep 2034"
#> [295] "Oct 2034" "Nov 2034" "Dec 2034" "Jan 2035" "Feb 2035" "Mar 2035"
#> [301] "Apr 2035" "May 2035" "Jun 2035" "Jul 2035" "Aug 2035" "Sep 2035"
#> [307] "Oct 2035" "Nov 2035" "Dec 2035" "Jan 2036" "Feb 2036" "Mar 2036"
#> [313] "Apr 2036" "May 2036" "Jun 2036" "Jul 2036" "Aug 2036" "Sep 2036"
#> [319] "Oct 2036" "Nov 2036" "Dec 2036" "Jan 2037" "Feb 2037" "Mar 2037"
#> [325] "Apr 2037" "May 2037" "Jun 2037" "Jul 2037" "Aug 2037" "Sep 2037"
#> [331] "Oct 2037" "Nov 2037" "Dec 2037" "Jan 2038" "Feb 2038" "Mar 2038"
#> [337] "Apr 2038" "May 2038" "Jun 2038" "Jul 2038" "Aug 2038" "Sep 2038"
#> [343] "Oct 2038" "Nov 2038" "Dec 2038" "Jan 2039" "Feb 2039" "Mar 2039"
#> [349] "Apr 2039" "May 2039" "Jun 2039" "Jul 2039" "Aug 2039" "Sep 2039"
#> [355] "Oct 2039" "Nov 2039" "Dec 2039" "Jan 2040" "Feb 2040" "Mar 2040"

创建于2023-03-24使用reprex v2.0.2

km0tfn4u

km0tfn4u3#

如果将by="1 month"替换为by=1/12,则问题中的seq命令将正常工作。(例如,添加1/12得到下个月,等等),并且不需要显式地中间转换为Date。是年份加分数,其中分数是0,1/12,2/12,...,11/12,代表一年中的12个月,因此:

library(zoo)

seq(as.yearmon("2010-04-01"), as.yearmon("2040-03-01"), by = 1/12)
##   [1] "Apr 2010" "May 2010" "Jun 2010" "Jul 2010" "Aug 2010" "Sep 2010"
##   [7] "Oct 2010" "Nov 2010" "Dec 2010" "Jan 2011" "Feb 2011" "Mar 2011"
##  [13] "Apr 2011" "May 2011" "Jun 2011" "Jul 2011" "Aug 2011" "Sep 2011"
## ...snip...
## [343] "Oct 2038" "Nov 2038" "Dec 2038" "Jan 2039" "Feb 2039" "Mar 2039"
## [349] "Apr 2039" "May 2039" "Jun 2039" "Jul 2039" "Aug 2039" "Sep 2039"
## [355] "Oct 2039" "Nov 2039" "Dec 2039" "Jan 2040" "Feb 2040" "Mar 2040"

实际上不需要-01,因此也可以写成:

seq(as.yearmon("2010-04"), as.yearmon("2040-03"), by = 1/12)
8qgya5xd

8qgya5xd4#

你可以使用我的软件包timeplyr中的time_seq()来获得更灵活的开始日期,它也没有混合日期和日期时间的问题。

# remotes::install_github("NicChr/timeplyr") 
library(timeplyr)
time_seq("2023-03-01", "2023-12-01", by = "month")
#>  [1] "2023-03-01" "2023-04-01" "2023-05-01" "2023-06-01" "2023-07-01"
#>  [6] "2023-08-01" "2023-09-01" "2023-10-01" "2023-11-01" "2023-12-01"
library(zoo)
as.yearmon(time_seq("2023-03-01", "2023-12-01", by = "month"))
#>  [1] "Mar 2023" "Apr 2023" "May 2023" "Jun 2023" "Jul 2023" "Aug 2023"
#>  [7] "Sep 2023" "Oct 2023" "Nov 2023" "Dec 2023"

time_seq("2023-03-31", "2023-12-01", by = "month") # This works
#> [1] "2023-03-31" "2023-04-30" "2023-05-31" "2023-06-30" "2023-07-31"
#> [6] "2023-08-31" "2023-09-30" "2023-10-31" "2023-11-30"
seq(as.Date("2023-03-31"), as.Date("2023-12-01"), by = "month") # This doesn't
#> [1] "2023-03-31" "2023-05-01" "2023-05-31" "2023-07-01" "2023-07-31"
#> [6] "2023-08-31" "2023-10-01" "2023-10-31" "2023-12-01"

创建于2023-03-24使用reprex v2.0.2

相关问题