底部发布的代码很好地使用包tidyr填充了一个数据帧,以便所有ID在Period定义为月数的情况下以相同的期间数结束(以下代码中的"Period_1").基本数据帧testDF的ID为1,有5个周期,ID为50和60,每个只有3个周期.tidyr代码为ID 50和60创建额外的句点("Period_1"),因此它们也有5个Period_1.该代码向下复制"Bal"和"State"字段,以便所有ID都以相同数量的Period_1结束,这是正确的.

但是,我如何以同样的方式扩展"PERIOD_2"的日历月表达式,如下面所示?

enter image description here

代码:

library(tidyr)

testDF <-
  data.frame(
    ID = as.numeric(c(rep(1,5),rep(50,3),rep(60,3))),
    Period_1 = as.numeric(c(1:5,1:3,1:3)),
    Period_2 = c("2012-06","2012-07","2012-08","2012-09","2012-10","2013-06","2013-07","2013-08","2012-01","2012-02","2012-03"),
    Bal = as.numeric(c(rep(10,5),21:23,36:34)),
    State = c("XX","AA","BB","CC","XX","AA","BB","CC","SS","XX","AA")
  )

testDFextend <-
  testDF %>%
  tidyr::complete(ID, nesting(Period_1)) %>%
  tidyr::fill(Bal, State, .direction = "down")

testDFextend

Edit: rolling from one year to the next

更好的OP示例应该是Period 2 = c("2012-06","2012-07","2012-08","2012-09","2012-10","2013-06","2013-07","2013-08","2012-10","2012-11","2012-12"),这提供了一个延长Period_2会导致滚动到下一年的示例.下面我添加到下面的tidyr/dplyr答案中,以正确地转到年份:

library(tidyr)
library(dplyr)

testDF <-
  data.frame(
    ID = as.numeric(c(rep(1,5),rep(50,3),rep(60,3))),
    Period_1 = as.numeric(c(1:5,1:3,1:3)),
    Period_2 = c("2012-06","2012-07","2012-08","2012-09","2012-10","2013-06","2013-07","2013-08","2012-10","2012-11","2012-12"),
    Bal = as.numeric(c(rep(10,5),21:23,36:34)),
    State = c("XX","AA","BB","CC","XX","AA","BB","CC","SS","XX","AA")
  )

testDFextend <-
  testDF %>%
  tidyr::complete(ID, nesting(Period_1)) %>%
  tidyr::fill(Bal, State, .direction = "down")

testDFextend %>% 
  separate(Period_2, into = c("year", "month"), convert = TRUE) %>% 
  fill(year) %>% 
  group_by(ID) %>% 
  mutate(month = sprintf("%02d", zoo::na.spline(month))) %>% 
  unite("Period_2", year, month, sep = "-") %>% 
# Now I add the below lines:
  separate(Period_2, into = c("year", "month"), convert = TRUE) %>% 
  mutate(month = as.integer(sprintf("%02d", zoo::na.spline(month)))) %>%
  mutate(year1 = ifelse(month > 12, year+trunc(month/12), year)) %>%
  mutate(month1 = ifelse(month > 12 & month%%12!= 0, month%%12, month)) %>%
  mutate(month1 = ifelse(month1 < 10, paste0(0,month1),month1)) %>%
  unite("Period_2", year1, month1, sep = "-") %>%
  select("ID","Period_1","Period_2","Bal","State")

推荐答案

对于每个ID,将Period_2转换为Year Mon类.这代表没有日的年和月.在内部,它使用年份+分数,其中分数=0、1/12、...、11/12表示12个月.使用seq将其扩展.然后将其转换回字符或省略第format行以将结果保留为一年对象.

library(dplyr, exclude = c("filter", "lag"))
library(zoo)

testDFextend %>%
  group_by(ID) %>%
  mutate(Period_2 = as.yearmon(first(Period_2)) + seq(0, by=1/12, length=n())) %>%
  mutate(Period_2 = format(Period_2, "%Y-%m")) %>%
  ungroup

给予:

# A tibble: 15 × 5
      ID Period_1 Period_2   Bal State
   <dbl>    <dbl> <chr>    <dbl> <chr>
 1     1        1 2012-06     10 XX   
 2     1        2 2012-07     10 AA   
 3     1        3 2012-08     10 BB   
 4     1        4 2012-09     10 CC   
 5     1        5 2012-10     10 XX   
 6    50        1 2013-06     21 AA   
 7    50        2 2013-07     22 BB   
 8    50        3 2013-08     23 CC   
 9    50        4 2013-09     23 CC   
10    50        5 2013-10     23 CC   
11    60        1 2012-01     36 SS   
12    60        2 2012-02     35 XX   
13    60        3 2012-03     34 AA   
14    60        4 2012-04     34 AA   
15    60        5 2012-05     34 AA   

R相关问答推荐

从R中的另一个包扩展S3类的正确方法是什么

在"gt"表中添加第二个"groupname_col",而不连接列值

计算时间段的ECDF(R)

汇总数据表中两个特定列条目的值

使用列/行匹配将两个不同维度的矩阵相加

try 将 colored颜色 编码添加到ggploly的标题中

使用`Watch()`和`renderUI()`时,不再满足仍出现在SHILINY AFTER条件中的条件输入

用两种 colored颜色 填充方框图

安全地测试文件是否通过R打开

如何在R中改变fviz_pca_biplot中圆的边界线的 colored颜色 ?

如何计算增加10米(0.01公里)的行?

远离理论值的伽马密度曲线下面积的近似

ggplot R:X,Y,Z使用固定/等距的X,Y坐标绘制六边形热图

将统计检验添加到GGPUBR中的盒图,在R

有没有办法将基于每个值中出现的两个关键字或短语的字符串向量重新编码为具有这两个值的新向量?

我需要使用ggplot2制作堆叠条形图

使用R、拼图和可能的网格包绘制两个地块的公共垂直线

如何将字符类对象中的数据转换为R中的字符串

R:水平旋转图

只有当我在循环的末尾放置一条print语句时,Foreach才会给出预期的输出