我有一个如下的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 转换为时间序列对象。不幸的是,我总是遇到错误,因为该函数似乎喜欢使用年份时间序列,有人对我如何让它工作有什么建议吗?非常感谢任何建议。
1条答案
按热度按时间p8h8hvxi1#
转化为zoo对象,然后转化为ts:
给予