Мы можем использовать na.locf0
из zoo
library(dplyr)
library(zoo)
df1 %>%
group_by(ID, year) %>%
mutate_at(vars(x, y), list(~ na.locf0(na.locf0(., fromLast = TRUE))))
# A tibble: 10 x 5
# Groups: ID, year [6]
# ID year month x y
# <chr> <int> <int> <int> <int>
# 1 A 2014 3 2 NA
# 2 B 2010 2 3 21
# 3 B 2010 5 3 21
# 4 B 2011 2 2 25
# 5 B 2011 5 2 25
# 6 C 2012 5 NA 23
# 7 C 2013 2 2 22
# 8 C 2013 5 2 22
# 9 C 2014 2 1 30
#10 C 2014 11 1 30
Или использовать fill
из tidyr
library(tidyr)
df1 %>%
group_by(ID, year) %>%
fill(x, y, .direction = 'up') %>%
fill(x, y)
Для получения окончательного результата
df1 %>%
group_by(ID, year) %>%
fill(x, y, .direction = 'up') %>%
slice(1)
# A tibble: 6 x 5
# Groups: ID, year [6]
# ID year month x y
# <chr> <int> <int> <int> <int>
#1 A 2014 3 2 NA
#2 B 2010 2 3 21
#3 B 2011 2 2 25
#4 C 2012 5 NA 23
#5 C 2013 2 2 22
#6 C 2014 2 1 30
данные
df1 <- structure(list(ID = c("A", "B", "B", "B", "B", "C", "C", "C",
"C", "C"), year = c(2014L, 2010L, 2010L, 2011L, 2011L, 2012L,
2013L, 2013L, 2014L, 2014L), month = c(3L, 2L, 5L, 2L, 5L, 5L,
2L, 5L, 2L, 11L), x = c(2L, 3L, NA, 2L, NA, NA, 2L, NA, 1L, NA
), y = c(NA, NA, 21L, NA, 25L, 23L, NA, 22L, NA, 30L)),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"))