【发布时间】:2021-08-17 11:43:38
【问题描述】:
我一直在从事一个包含时间序列的项目。我的问题是我没有每一年的信息,而是那个时期的信息。我基本上想复制每一行,只要持续时间:在 n 年期间,我想创建 (n-1) 具有完全相同信息的新行。到目前为止,一切顺利。
stack = data.frame(c("ville","commune","université","pole emploi", "ministère","collège"),
c(2014,2015,2016,2014,2015,2014),
c(5,3,2,6,4,1))
colnames(stack) = c("benefit recipient","beginning year", "length of the period")
->
b = stack$`beginning year`
stack2 = stack[rep(rownames(stack),b),]
现在我想做的是将开始年份修改为当前年份。所以我想在每一行中添加一年后的一年。为了可视化它,这里有一些我手动执行的代码(也是我在真实项目中拥有的和想要的截图。
stack3 = data.frame(c("ville","ville","ville","ville","ville","commune","commune","commune","université","université","pole emploi","pole emploi","pole emploi","pole emploi","pole emploi","pole emploi", "ministère","ministère","ministère","ministère","collège"),
c(2014,2015,2016,2017,2018,2015,2016,2017,2016,2017,2014,2015,2016,2017,2018,2019,2015,2016,2017,2018,2014),
c(5,5,5,5,5,3,3,3,2,2,6,6,6,6,6,6,4,4,4,4,1))
colnames(stack3) = c("benefit recipient","effective year", "length of the period")
到目前为止,我的想法是拆分我的周期并将这个新向量的值添加到我的表中。我试过这个功能:
c = c(0:(b-1))
但它不起作用,我有消息在 0:(b - 1) 中:数值表达式有 6 个元素:只使用第一个元素。很遗憾,因为它完全符合我的要求,但仅适用于第一个元素......
你知道我该如何解决吗?
非常感谢您的宝贵时间! What I have What I would like to have
【问题讨论】:
-
似乎某些 不同 行代码给出了该错误,您没有向我们展示。究竟是什么代码给出了这个错误。
标签: r list time-series