R语言 将简单的df转换为按年聚合的ts对象

jexiocij  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(122)

我有一个如下的df:

structure(list(Type = c("Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", 
"Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", 
"Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", 
"Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", 
"Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", "Type 3", 
"Type 3", "Type 3", "Type 3", "Type 4", "Type 4", "Type 4", "Type 4", 
"Type 4", "Type 4", "Type 4", "Type 4", "Type 4", "Type 4", "Type 4", 
"Type 4", "Type 4", "Type 4", "Type 4", "Type 4", "Type 4"), 
    Year = c(1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 
    1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 
    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
    2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 
    2021, 2022, 2023, 1986, 1987, 1991, 1992, 1993, 1994, 1995, 
    1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 
    2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 
    2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 1982, 1984, 
    1985, 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 
    1998, 1999, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
    2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 
    2020, 2021, 2022, 2023, 2002, 2004, 2005, 2008, 2009, 2010, 
    2011, 2012, 2013, 2014, 2016, 2017, 2018, 2020, 2021, 2022, 
    2023), Rolling_Sum = c(2, 1.65, 2.8, 0.65, 2.1, 4.7, 0.8, 
    1.65, 3.95, 7.3, 13.45, 7.6, 11.35, 14.25, 9.475, 5.65, 6.22, 
    5.945, 8.22, 9.102, 14.35, 17.7, 27.391, 29.15, 35.2, 63.627, 
    94.592869, 71.638245, 71.616951, 70.640088, 60.114466, 54.180581, 
    49.588879, 55.028252, 50.848614, 48.744727, 32.235111, 105.109657, 
    88.073521, 82.361093, 6, 0.05, 0.1, 0.65, 5.7, 20.15, 6.6, 
    11.7, 6.7, 9.725, 4.95, 3.125, 0.435, 3.37, 9.225, 13.575, 
    8.925, 13.4, 19.05, 13.5, 38.871, 79.895962, 49.904916, 43.987515, 
    43.12883, 36.506245, 38.223299, 30.297244, 32.444623, 32.109065, 
    27.894704, 31.563774, 147.750757, 79.620748, 59.666394, 7.898425, 
    1, 0.4, 0.1, 0.4, 0.65, 0.2, 0.5, 0.4, 0.1, 0.1, 0.8, 2.35, 
    0.35, 2.5, 3.669, 18.925, 9, 18.49, 10.5, 10.25, 44.414, 
    76.488204, 46.723932, 49.309076, 54.921755, 50.647539, 41.255551, 
    31.381031, 37.768795, 29.27807, 28.434694, 35.099662, 128.568204, 
    48.105376, 40.714246, 3.5, 0.661, 8.5, 2.829, 22.755, 20.187569, 
    8.5297, 7.447, 6.04, 1.75, 3.428, 1.597, 5.966365, 2.05, 
    35.301736, 15.749993, 33.923156, 4.44075)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -129L), groups = structure(list(
    Type = c("Type 1", "Type 2", "Type 3", "Type 4"), .rows = structure(list(
        1:41, 42:76, 77:112, 113:129), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), .drop = TRUE))

我已经按照年份和类型有效地聚合了一个大得多的数据集。现在我想使用ts()函数和stats库将 Dataframe 转换为时间序列对象。不幸的是,我总是遇到错误,因为该函数似乎喜欢使用年份时间序列,有人对我如何让它工作有什么建议吗?非常感谢任何建议。

p8h8hvxi

p8h8hvxi1#

转化为zoo对象,然后转化为ts:

library(zoo)
df |> read.zoo(index = "Year", split = "Type") |> as.ts()

给予

Time Series:
Start = 1981 
End = 2023 
Frequency = 1 
        Type 1     Type 2    Type 3    Type 4
1981   2.00000         NA        NA        NA
1982   1.65000         NA   1.00000        NA
1983   2.80000         NA        NA        NA
1984   0.65000         NA   0.40000        NA
1985   2.10000         NA   0.10000        NA
1986   4.70000   0.050000        NA        NA
1987   0.80000   0.100000   0.40000        NA
1988   1.65000         NA   0.65000        NA
1989        NA         NA        NA        NA
1990        NA         NA        NA        NA
1991   3.95000   0.650000   0.20000        NA
1992   7.30000   5.700000   0.50000        NA
1993  13.45000  20.150000   0.40000        NA
1994   7.60000   6.600000   0.10000        NA
1995  11.35000  11.700000   0.10000        NA
1996  14.25000   6.700000   0.80000        NA
...snip...

相关问题