0

I have this dataframe:

df <- structure(list(x = c(1, 5, 6, 7, 8), y = c("a", "e", "f", "g", 
"h")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-5L))

      x y    
  <dbl> <chr>
1     1 a    
2     5 e    
3     6 f    
4     7 g    
5     8 h 

With complete from tidyr package: I can do:

df %>% 
  complete(x = full_seq(min(x):max(x), 1))

      x y    
  <dbl> <chr>
1     1 a    
2     2 NA   
3     3 NA   
4     4 NA   
5     5 e    
6     6 f    
7     7 g    
8     8 h

Now I would like to do the same with the y column:

df %>% 
  complete(y = full_seq(min(y):max(y), 1))

This obviously will not work.

How can I use complete from tidyr package for alphabetical order?

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Related: [Create a sequence between two letters](https://stackoverflow.com/questions/53476833/create-a-sequence-between-two-letters) – markus Aug 31 '22 at 20:24

1 Answers1

1

I don't think that's possible, especially because except in the case of 1 letter, it would not be possible to complete strings with more than one letter. You can still use the letters data set:

df %>% 
  complete(y = letters[full_seq(min(x):max(x), 1)])

or, to be entirely relying on y:

df %>% 
  complete(y = letters[which(letters == min(y)):which(letters == max(y))])
  y         x
1 a         1
2 b        NA
3 c        NA
4 d        NA
5 e         5
6 f         6
7 g         7
8 h         8
Maël
  • 45,206
  • 3
  • 29
  • 67