r-如何在阶梯式数据层次结构上填写值

Is there an elegant/tidy way to fill in the data if there are non-null values to the right? I have a wonky work-around but wanted to know if there was a nice `dplyr` way to do this.

``````actual <-
tibble(
a = c("A", NA, NA, NA, NA, NA, NA, "B", NA, NA, NA),
b = c(NA, "A", NA, NA, NA, "C", NA, NA, "E", NA, NA),
c = c(NA, NA, "B", NA, NA, NA, "D", NA, NA, "F", "G"),
d = c(NA, NA, NA, "C", "D", NA, NA, NA, NA, NA, NA)
)

desired <-
tibble(
w = c("A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B"),
x = c(NA, "A", "A", "A", "A", "C", "C", NA, "E", "E", "E"),
y = c(NA, NA, "B", "B", "B", NA, "D", NA, NA, "F", "G"),
z = c(NA, NA, NA, "C", "D", NA, NA, NA, NA, NA, NA)
)

``````

• qqgsd 回复

We can use `fill` from `tidyr` together with `dplyr` like the following.

``````library(dplyr)
library(tidyr)

dat <- actual %>%
fill(a) %>%
group_by(a) %>%
fill(b) %>%
group_by(b) %>%
fill(c) %>%
group_by(c) %>%
fill(d) %>%
ungroup()

print(dat)
# # A tibble: 11 x 4
#    a     b     c     d
#    <chr> <chr> <chr> <chr>
#  1 A     NA    NA    NA
#  2 A     A     NA    NA
#  3 A     A     B     NA
#  4 A     A     B     C
#  5 A     A     B     D
#  6 A     C     NA    NA
#  7 A     C     D     NA
#  8 B     NA    NA    NA
#  9 B     E     NA    NA
# 10 B     E     F     NA
# 11 B     E     G     NA
``````