简体   繁体   English

动物园图书馆中年/季度至日期转换的错误

[英]error with year/quarter to date conversion in zoo library

I have a dataframe of prices and dates in the form 我有以下形式的价格和日期数据框

```str(prices)
'data.frame':   83 obs. of  5 variables:
 $ Year         : int  1995 1995 1995 1995 1996 1996 1996 1996 1997 1997 ...
 $ Quarter      : int  1 2 3 4 1 2 3 4 1 2 ...
 $ US.avg       : num  297 297 288 288 284 ...
 $ inflation_adj: num  467 463 447 446 434 ...
 $ year         : chr  "1995/Q1" "1995/Q2" "1995/Q3" "1995/Q4" 

``` ```

 US.avg = c(296.9, 296.8, 287.51, 287.78, 283.97, 275.78, 
    269.49, 278.33, 283.4, 289.44, 282.27, 293.51, 304.74, 300.97, 
    315.25, 316.18, 331.74, 329.34, 317.22, 317.93, 340.23, 339.16, 
    336.66, 340.08, 347.69, 328.67, 303.02, 299.81, 320.02, 317.93, 
    303.3, 308.85, 319.19, 314.52, 312.39, 315.77, 320.23, 309.45, 
    296.54, 297.28, 301.39, 306.68, 305.91, 314.76, 323.34, 341.58, 
    330.12, 318.16, 317.84, 325.39, 327.56, 329.77, 333.29, 346.99, 
    358.93, 345.42, 313.82, 301.82, 306.95, 319.85, 328.12, 340.72, 
    339.71, 334.78, 355.72, 369.68, 360.74, 368.39, 372.83, 384.67, 
    366.97, 374.23, 377.93, 378, 390.04, 382.04, 382.15, 395.62, 
    396.37, 392.66, 388.32, 385.91, 371.72), inflation_adj = c(466.61, 
    463.1, 446.56, 446.09, 433.97, 418.76, 406.36, 417.57, 421.47, 
    429.63, 416.66, 432.98, 447.05, 439.35, 458.52, 459.02, 478.4, 
    471.5, 449.56, 449.5, 472.87, 468.11, 461.18, 465.06, 469.52, 
    439.36, 404.39, 403.72, 425.88, 420.51, 398.72, 406.25, 412.32, 
    407.4, 401.36, 407.69, 406.61, 388.15, 371.56, 371.71, 371, 
    375.19, 366.14, 380.57, 385.07, 400.58, 387.14, 375.15, 368.29, 
    371.6, 373.84, 373.59, 371.41, 377.33, 390.36, 390.96, 351.05, 
    332.96, 338.19, 352.43, 358.74, 371.95, 370.05, 363.44, 378.77, 
    389.7, 378.32, 388.42, 386.73, 398.86, 377.33, 387.83, 386.33, 
    385.19, 396.36, 390.07, 384.83, 394.96, 396.22, 397.9, 391.32, 
    384.79, 371.72), year = c("1995/Q1", "1995/Q2", "1995/Q3", 
    "1995/Q4", "1996/Q1", "1996/Q2", "1996/Q3", "1996/Q4", "1997/Q1", 
    "1997/Q2", "1997/Q3", "1997/Q4", "1998/Q1", "1998/Q2", "1998/Q3", 
    "1998/Q4", "1999/Q1", "1999/Q2", "1999/Q3", "1999/Q4", "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", "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", "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", "2013/Q3", 
    "2013/Q4", "2014/Q1", "2014/Q2", "2014/Q3", "2014/Q4", "2015/Q1", 
    "2015/Q2", "2015/Q3")), .Names = c("Year", "Quarter", "US.avg", 
"inflation_adj", "year"), row.names = c(NA, -83L), class = "data.frame")

I have tried using the zoo library to convert the year column into a date. 我尝试使用动物园图书馆将year列转换为日期。 When I implement the code 当我实现代码时

prices$year = as.Date(as.yearqtr(prices$year, format = "%y/Q%q")) I get an output of NAs. prices$year = as.Date(as.yearqtr(prices$year, format = "%y/Q%q"))我得到了NA的输出。 Is there any way to fix this? 有没有什么办法解决这一问题? I don't see where my mistake is. 我看不出我的错误在哪里。

Edit: 编辑:

structure(list(US.avg = c(296.9, 296.8, 287.51, 287.78, 283.97, 
275.78), inflation_adj = c(466.61, 463.1, 446.56, 446.09, 433.97, 
418.76), year = c("1995/Q1", "1995/Q2", "1995/Q3", "1995/Q4", 
"1996/Q1", "1996/Q2")), .Names = c("US.avg", "inflation_adj", 
"year"), row.names = c(NA, 6L), class = "data.frame")

I can't reproduce any problem. 我无法重现任何问题。 The following works for me. 以下对我有用。

DF <- data.frame(year = c("1995/Q1", "1995/Q2", "1995/Q3", 
"1995/Q4", "1996/Q1", "1996/Q2", "1996/Q3", "1996/Q4", "1997/Q1", 
"1997/Q2", "1997/Q3", "1997/Q4", "1998/Q1", "1998/Q2", "1998/Q3", 
"1998/Q4", "1999/Q1", "1999/Q2", "1999/Q3", "1999/Q4", "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", "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", "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", "2013/Q3", 
"2013/Q4", "2014/Q1", "2014/Q2", "2014/Q3", "2014/Q4", "2015/Q1", 
"2015/Q2", "2015/Q3"))

 library(zoo)
 as.Date(as.yearqtr(DF$year, format = "%Y/Q%q"))

giving: 给予:

 [1] "1995-01-01" "1995-04-01" "1995-07-01" "1995-10-01" "1996-01-01"
 [6] "1996-04-01" "1996-07-01" "1996-10-01" "1997-01-01" "1997-04-01"
[11] "1997-07-01" "1997-10-01" "1998-01-01" "1998-04-01" "1998-07-01"
[16] "1998-10-01" "1999-01-01" "1999-04-01" "1999-07-01" "1999-10-01"
[21] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01"
[26] "2001-04-01" "2001-07-01" "2001-10-01" "2002-01-01" "2002-04-01"
[31] "2002-07-01" "2002-10-01" "2003-01-01" "2003-04-01" "2003-07-01"
[36] "2003-10-01" "2004-01-01" "2004-04-01" "2004-07-01" "2004-10-01"
[41] "2005-01-01" "2005-04-01" "2005-07-01" "2005-10-01" "2006-01-01"
[46] "2006-04-01" "2006-07-01" "2006-10-01" "2007-01-01" "2007-04-01"
[51] "2007-07-01" "2007-10-01" "2008-01-01" "2008-04-01" "2008-07-01"
[56] "2008-10-01" "2009-01-01" "2009-04-01" "2009-07-01" "2009-10-01"
[61] "2010-01-01" "2010-04-01" "2010-07-01" "2010-10-01" "2011-01-01"
[66] "2011-04-01" "2011-07-01" "2011-10-01" "2012-01-01" "2012-04-01"
[71] "2012-07-01" "2012-10-01" "2013-01-01" "2013-04-01" "2013-07-01"
[76] "2013-10-01" "2014-01-01" "2014-04-01" "2014-07-01" "2014-10-01"
[81] "2015-01-01" "2015-04-01" "2015-07-01"

Also: 也:

DF2 <- structure(list(US.avg = c(296.9, 296.8, 287.51, 287.78, 283.97, 
275.78), inflation_adj = c(466.61, 463.1, 446.56, 446.09, 433.97, 
418.76), year = c("1995/Q1", "1995/Q2", "1995/Q3", "1995/Q4", 
"1996/Q1", "1996/Q2")), .Names = c("US.avg", "inflation_adj", 
"year"), row.names = c(NA, 6L), class = "data.frame")


as.Date(as.yearqtr(DF2$year, format = "%Y/Q%q"))

gives: 给出:

[1] "1995-01-01" "1995-04-01" "1995-07-01" "1995-10-01" "1996-01-01"
[6] "1996-04-01"

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

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