library(dplyr)
lux <- terra::vect(system.file("ex/lux.shp", package = "terra"))
# Leave some blanks for demo purporses
lux_blnk <- lux %>%
mutate(NAME_1 = if_else(NAME_1 != NAME_2, NA, NAME_2))
as_tibble(lux_blnk)
# `fill()` defaults to replacing missing data from top to bottom
lux_blnk %>%
fill(NAME_1) %>%
as_tibble()
# direction = "up"
lux_blnk %>%
fill(NAME_1, .direction = "up") %>%
as_tibble()
# Grouping and downup - will restore the initial state
lux_blnk %>%
group_by(ID_1) %>%
fill(NAME_1, .direction = "downup") %>%
as_tibble()
Run the code above in your browser using DataLab