0

Example data:

ladder = data.frame(x = 1:10)

df <- data.frame(
  id = rep('a', 5),
  x = 6:10
)

> df
  id  x
1  a  6
2  a  7
3  a  8
4  a  9
5  a 10

I want to extend df so that all groups have the full sequence of values from 1:10:

Attempt:

> df |> group_by(id) |> full_join(ladder, by = 'x')
# A tibble: 10 × 2
# Groups:   id [2]
   id        x
   <chr> <int>
 1 a         6
 2 a         7
 3 a         8
 4 a         9
 5 a        10
 6 NA        1
 7 NA        2
 8 NA        3
 9 NA        4
10 NA        5

Close, but I want the a to fill down where there were missing values, not NA. Desired outcome:

 df
   id  x
1   a  1
2   a  2
3   a  3
4   a  4
5   a  5
6   a  6
7   a  7
8   a  8
9   a  9
10  a 10

How can I join in such a way so as to ensure that for each group, there is at least 1:10 of x and where expanding, populate with the grouping vars not NA?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299

0 Answers0