1

我有以下数据框:

> dput(gdp_fecha)
structure(list(Quarters = structure(c(10957, 11048, 11139, 11231, 
11323, 11413, 11504, 11596, 11688, 11778, 11869, 11961, 12053, 
12143, 12234, 12326, 12418, 12509, 12600, 12692, 12784, 12874, 
12965, 13057, 13149, 13239, 13330, 13422, 13514, 13604, 13695, 
13787, 13879, 13970, 14061, 14153, 14245, 14335, 14426, 14518, 
14610, 14700, 14791, 14883, 14975, 15065, 15156, 15248, 15340, 
15431, 15522, 15614, 15706, 15796, 15887, 15979, 16071, 16161, 
16252, 16344, 16436, 16526, 16617, 16709, 16801, 16892, 16983, 
17075, 17167, 17257, 17348, 17440, 17532, 17622, 17713, 17805, 
17897, 17987, 18078, 18170, 18262, 18353, 18444), class = "Date")), row.names = c(NA, 
-83L), class = "data.frame")

(从 2000 年 1 月 1 日到 2020 年 7 月 1 日,休息 3 个月)

我正在尝试使用包中的quarters功能lubridate

我希望日期看起来像这样:2000 Q1,2000 Q3,等等......

我正在尝试使用此代码,但出现此错误:

> gdp_fecha$Quarters <- quarter(gdp_fecha$Quarters, with_year=TRUE)
Error in quarter(gdp_fecha$Quarters, with_year = TRUE) : 
  unused argument (with_year = TRUE)

我检查了 lubridate 文档,看起来就像那样。

是否有任何解决此错误的方法,或任何其他方式来获取我想要的日期?

提前致谢!

4

1 回答 1

2

具有with_year参数的函数 isquarter和 notquarters

library(lubridate)
quarter(gdp_fecha$Quarters, with_year=TRUE)
 [1] 2000.1 2000.2 2000.3 2000.4 2001.1 2001.2 2001.3 2001.4 2002.1 2002.2 2002.3 2002.4 2003.1 2003.2 2003.3 2003.4 2004.1 2004.2 2004.3 2004.4 2005.1 2005.2 2005.3 2005.4 2006.1 2006.2 2006.3
[28] 2006.4 2007.1 2007.2 2007.3 2007.4 2008.1 2008.2 2008.3 2008.4 2009.1 2009.2 2009.3 2009.4 2010.1 2010.2 2010.3 2010.4 2011.1 2011.2 2011.3 2011.4 2012.1 2012.2 2012.3 2012.4 2013.1 2013.2
[55] 2013.3 2013.4 2014.1 2014.2 2014.3 2014.4 2015.1 2015.2 2015.3 2015.4 2016.1 2016.2 2016.3 2016.4 2017.1 2017.2 2017.3 2017.4 2018.1 2018.2 2018.3 2018.4 2019.1 2019.2 2019.3 2019.4 2020.1
[82] 2020.2 2020.3

但是,它以数字形式返回值。如果我们需要某种格式的输出,也format可以通过sprintf提取year

sprintf('%d-%s', year(gdp_fecha$Quarters), quarters(gdp_fecha$Quarters))
 [1] "2000-Q1" "2000-Q2" "2000-Q3" "2000-Q4" "2001-Q1" "2001-Q2" "2001-Q3" "2001-Q4" "2002-Q1" "2002-Q2" "2002-Q3" "2002-Q4" "2003-Q1" "2003-Q2" "2003-Q3" "2003-Q4" "2004-Q1" "2004-Q2"
[19] "2004-Q3" "2004-Q4" "2005-Q1" "2005-Q2" "2005-Q3" "2005-Q4" "2006-Q1" "2006-Q2" "2006-Q3" "2006-Q4" "2007-Q1" "2007-Q2" "2007-Q3" "2007-Q4" "2008-Q1" "2008-Q2" "2008-Q3" "2008-Q4"
[37] "2009-Q1" "2009-Q2" "2009-Q3" "2009-Q4" "2010-Q1" "2010-Q2" "2010-Q3" "2010-Q4" "2011-Q1" "2011-Q2" "2011-Q3" "2011-Q4" "2012-Q1" "2012-Q2" "2012-Q3" "2012-Q4" "2013-Q1" "2013-Q2"
[55] "2013-Q3" "2013-Q4" "2014-Q1" "2014-Q2" "2014-Q3" "2014-Q4" "2015-Q1" "2015-Q2" "2015-Q3" "2015-Q4" "2016-Q1" "2016-Q2" "2016-Q3" "2016-Q4" "2017-Q1" "2017-Q2" "2017-Q3" "2017-Q4"
[73] "2018-Q1" "2018-Q2" "2018-Q3" "2018-Q4" "2019-Q1" "2019-Q2" "2019-Q3" "2019-Q4" "2020-Q1" "2020-Q2" "2020-Q3"

另一种选择as.yearqtr来自zoo

format(zoo::as.yearqtr(gdp_fecha$Quarters), '%Y-Q%q')
 [1] "2000-Q1" "2000-Q2" "2000-Q3" "2000-Q4" "2001-Q1" "2001-Q2" "2001-Q3" "2001-Q4" "2002-Q1" "2002-Q2" "2002-Q3" "2002-Q4" "2003-Q1" "2003-Q2" "2003-Q3" "2003-Q4" "2004-Q1" "2004-Q2"
[19] "2004-Q3" "2004-Q4" "2005-Q1" "2005-Q2" "2005-Q3" "2005-Q4" "2006-Q1" "2006-Q2" "2006-Q3" "2006-Q4" "2007-Q1" "2007-Q2" "2007-Q3" "2007-Q4" "2008-Q1" "2008-Q2" "2008-Q3" "2008-Q4"
[37] "2009-Q1" "2009-Q2" "2009-Q3" "2009-Q4" "2010-Q1" "2010-Q2" "2010-Q3" "2010-Q4" "2011-Q1" "2011-Q2" "2011-Q3" "2011-Q4" "2012-Q1" "2012-Q2" "2012-Q3" "2012-Q4" "2013-Q1" "2013-Q2"
[55] "2013-Q3" "2013-Q4" "2014-Q1" "2014-Q2" "2014-Q3" "2014-Q4" "2015-Q1" "2015-Q2" "2015-Q3" "2015-Q4" "2016-Q1" "2016-Q2" "2016-Q3" "2016-Q4" "2017-Q1" "2017-Q2" "2017-Q3" "2017-Q4"
[73] "2018-Q1" "2018-Q2" "2018-Q3" "2018-Q4" "2019-Q1" "2019-Q2" "2019-Q3" "2019-Q4" "2020-Q1" "2020-Q2" "2020-Q3"
于 2021-07-09T00:05:01.623 回答