在R中获取季度并保留年份时出错

7jmck4yq  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(134)

我有以下 Dataframe :

> 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")

(from 2000年1月1日至2020年7月1日,中间休息3个月)
我正在尝试使用lubridate包中的quarters函数。
我希望日期看起来像这样:2000年第一季度、2000年第三季度等等...
我尝试使用此代码,但收到以下错误:

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

我查了润滑剂的文件,看起来就像这样。
有没有解决这个错误的方法,或者有没有其他方法可以得到我想要的日期?
先谢了!

j1dl9f46

j1dl9f461#

具有with_year参数的函数是quarter,而不是quarters

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

但是,它返回的值是numeric。如果我们需要一个格式的输出,通过提取year来在sprintf中执行format ing

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"

另一个选项是zoo中的as.yearqtr

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"
fhity93d

fhity93d2#

检查包版本:N/B with_year已过时;请改用type参数:例如:quarter(x, type = "year.quarter", fiscal_start = 1)
而不是quarter(x, with_year = TRUE, fiscal_start = 1)

相关问题