简体   繁体   English

在 R 中获取季度和保持年份时出错

[英]Error while getting quarter and keeping year in R

I have the following dataframe:我有以下 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 jan 1st 2000, to 1st july 2020, with breaks of 3 months) (从 2000 年 1 月 1 日到 2020 年 7 月 1 日,休息 3 个月)

I am trying to use the function quarters from lubridate package.我正在尝试使用来自lubridate package 的 function quarters

I want the dates to see like this: 2000 Q1, 2000 Q3, and so on...我希望日期看起来像这样:2000 Q1,2000 Q3,等等......

I was trying with this code, but I get this error:我正在尝试使用此代码,但出现此错误:

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

I checked the lubridate documentation, and appears just like that.我检查了 lubridate 文档,看起来就像那样。

Is there any solution to this error, or any other way to get the dates as I want?是否有任何解决此错误的方法,或任何其他方式来获取我想要的日期?

Thanks in advance!提前致谢!

The function having the with_year argument is quarter and not quarters具有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

but, it returns value as numeric.但是,它以数字形式返回值。 If we need the output in a format, do the format ing in sprintf by extracting the year as well如果我们需要某种格式的输出,也可以通过提取yearsprintf进行format

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"

another option is as.yearqtr from zoo另一种选择是来自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"

Check package version: N/B with_year has been deprecated;检查 package 版本:N/B with_year已被弃用; use the type parameter instead: For example: quarter(x, type = "year.quarter", fiscal_start = 1) and NOT quarter(x, with_year = TRUE, fiscal_start = 1)请改用type参数:例如: Quarter(x, type = "year.quarter", finance_start = 1) 而不是 quarter(x, with_year = TRUE, finance_start = 1)

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

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