【问题标题】:In 0:(b - 1) : numerical expression has 6 elements: only the first used在 0:(b - 1) :数值表达式有 6 个元素:只使用第一个
【发布时间】: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


【解决方案1】:

使用lapply()seq() 的解决方案:

b = stack$`beginning year`
c = stack$`length of the period`
stack2 = stack[rep(rownames(stack),b),]
stack2$`beginning year` = unlist(lapply(1:length(b), function(x) seq(b[x], b[x]+c[x]-1, by=1)))

【讨论】:

  • 非常感谢,它非常有效,但我会使用 Ronak 和 akrun 的方法,因为它看起来更容易。也许你的方法更强大,所以我记住了:)。
【解决方案2】:

我们可以使用rowwise

library(dplyr)
library(tidyr)
stack %>%
   rowwise %>%
   mutate(year = list(`beginning year`:(`beginning year` + 
                     `length of the period` - 1))) %>%
   unnest(year)

【讨论】:

  • 谢谢,效果很好,而且比我想象的更容易!
【解决方案3】:

您可以使用map2 创建序列,使用unnest 创建新行。

library(tidyverse)

stack %>%
  mutate(year = map2(`beginning year`, `beginning year` + `length of the period` - 1, seq)) %>%
  unnest(year)

#  `benefit recipient` `beginning year` `length of the period`  year
#   <chr>                          <dbl>                  <dbl> <int>
# 1 ville                           2014                      5  2014
# 2 ville                           2014                      5  2015
# 3 ville                           2014                      5  2016
# 4 ville                           2014                      5  2017
# 5 ville                           2014                      5  2018
# 6 commune                         2015                      3  2015
# 7 commune                         2015                      3  2016
# 8 commune                         2015                      3  2017
# 9 université                      2016                      2  2016
#10 université                      2016                      2  2017
# … with 11 more rows

【讨论】:

  • 谢谢,效果很好,而且比我想象的更容易!
猜你喜欢
  • 2018-02-07
  • 2018-08-21
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多