Home > Net >  Using complete to fill groups with NA to have same length as the maximum group
Using complete to fill groups with NA to have same length as the maximum group

Time:10-20

I have this dataframe:

df <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 3L), var = c("A", "B", 
"C", "B", "C", "C")), class = "data.frame", row.names = c(NA, 
-6L))

  id var
1  1   A
2  1   B
3  1   C
4  2   B
5  2   C
6  3   C

I would like to get this dataframe:

  id  var
1  1    A
2  1    B
3  1    C
4  2 <NA>
5  2    B
6  2    C
7  3 <NA>
8  3 <NA>
9  3    C

I would like to learn how to use complete or expand.grid in this situation

I have tried several ways but was not successful: One of my tries:

df %>% 
  complete(id, var, fill=list(NA))

CodePudding user response:

Create a duplicate column of 'var' and then do the complete on the other column, which makes the NA in the 'var' column and then remove the duplicate 'var' column

library(dplyr)
library(tidyr)
df %>% 
   mutate(var1 = var) %>% 
   complete(id, var1) %>% 
   select(-var1)

-output

# A tibble: 9 × 2
     id var  
  <int> <chr>
1     1 A    
2     1 B    
3     1 C    
4     2 <NA> 
5     2 B    
6     2 C    
7     3 <NA> 
8     3 <NA> 
9     3 C    
  • Related