Reputation: 1088
I have the following 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)
I am trying to use the function quarters
from lubridate
package.
I want the dates to see like this: 2000 Q1, 2000 Q3, and so on...
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.
Is there any solution to this error, or any other way to get the dates as I want?
Thanks in advance!
Upvotes: 1
Views: 507
Reputation: 1
Check package version:
N/B with_year
has been deprecated; 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)
Upvotes: 0
Reputation: 887971
The function having the with_year
argument is quarter
and not 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
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
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"
Upvotes: 3