Repeating sequence based on another value
我正在处理这样设置的数据结构:
数据
1 2 3 4 5 6 7 8 9 10 | structure(list(ID = c(1147L, 1147L, 1147L, 1147L, 1147L, 1147L, 1147L, 1147L, 1147L, 1147L, 1147L, 1147L, 4085L, 4085L, 4085L, 4085L, 4085L, 4085L, 4085L), year = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L), term = c(1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L), reportedFY = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), minFY = c(2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L)), class ="data.frame", row.names = c(NA, -19L)) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ID year term reportedFY minFY 1147 1 1 NA 2016 1147 1 3 NA 2016 1147 1 5 NA 2016 1147 2 1 NA 2016 1147 2 3 NA 2016 1147 2 5 NA 2016 1147 3 1 NA 2016 1147 3 3 NA 2016 1147 3 5 NA 2016 1147 4 1 NA 2016 1147 4 3 NA 2016 1147 4 5 NA 2016 4085 1 1 NA 2019 4085 1 3 NA 2019 4085 1 5 NA 2019 4085 2 1 NA 2019 4085 2 3 NA 2019 4085 2 5 NA 2019 4085 3 1 NA 2019 |
每个 ID(其中大约有 9000 个,每个重复 12 次,每年重复 3 次。我要做的是根据 minFY 为每个 ID 重复一个序列。所以 ID 1147 会有一个序列 2016,2017,2018,2019,其中每年重复 3 次。而 4085 将有一个序列 2019,2020,2021,2022,每个重复 3 次。
我一直在尝试使用与此类似的东西,这显然是行不通的:
1 | prog_structure2$reportedFY <- rep(c(minFY, minFY+1, minFY+2, minFY+3), time=9306, each=3) |
任何帮助将不胜感激!
基础R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | df$reportedFY <- ave(df$minFY, df$ID, FUN = function(x) rep(min(x) + 0:3, each = 3)) ID year term reportedFY minFY 1 1147 1 1 2016 2016 2 1147 1 3 2016 2016 3 1147 1 5 2016 2016 4 1147 2 1 2017 2016 5 1147 2 3 2017 2016 6 1147 2 5 2017 2016 7 1147 3 1 2018 2016 8 1147 3 3 2018 2016 9 1147 3 5 2018 2016 10 1147 4 1 2019 2016 11 1147 4 3 2019 2016 12 1147 4 5 2019 2016 13 4085 1 1 2019 2019 14 4085 1 3 2019 2019 15 4085 1 5 2019 2019 16 4085 2 1 2020 2019 17 4085 2 3 2020 2019 18 4085 2 5 2020 2019 19 4085 3 1 2021 2019 |
这应该适合你:
1 2 3 4 5 6 7 8 | library(dplyr) d <- data.frame('ID' = c(rep(1147, 12), rep(4085,12)), 'year' = rep(rep(1:4,each=3),2), 'term' = rep(c(1,3,5),8), 'reportedFY' = NA, 'minFY' = c(rep(2016, 12), rep(2019,12))) d <- d %>% group_by(ID) %>% mutate(reportedFY = rep(seq(first(minFY),(first(minFY)+3)),each = 3)) |