0

I'm having so trouble trying to fill in missing rows in a simple df

x <- data.frame( "Name" = c("John","Dora"), "Age" = c(21,15))

I always need a 2x2 dataframe and sometimes are John or Dora missing. I need the output to fill in John or Dora under Name with age of '0' when missing.

Here is what I'm trying

x[1, ] %>% 
       tidyr::complete(tidyr::nesting('John' , 'Dora'), fill = list('Age' = 0))

And it give me this error

Error: `by` can't contain join column `"John"`, `"Dora"` which is missing from RHS
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning messages:
1: In seq.default(along = x) :
partial argument match of 'along' to 'along.with'
2: In seq.default(along = x) :
partial argument match of 'along' to 'along.with'
Plot Norris
  • 49
  • 1
  • 9

1 Answers1

5

You can expand using factor levels in complete :

tidyr::complete(x, Name = factor(Name, levels = c('John', 'Dora')), 
                   fill = list(Age = 0))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213