Complete a data frame with missing combinations of data. Turns implicit missing values into explicit missing values.
complete_se(data, col_terms, fill = list(), env = parent.frame())
data | A data frame or tbl. |
---|---|
col_terms | A character vector of column names or expressions to complete by. |
fill | A list that for each variable supplies a single value to use instead of NA for missing combinations. |
env | The environment as an argument (in case the function is called from another function). |
The data frame with implicit missing values identified.
This is a standard evaluation interface for tidyr::complete()
. The purpose of the function is to be able to use a vector of characters (column names) as the argument for expanding the data frame.
# data frame used to illustrate tidyr::complete() df <- wrapr::build_frame( "group" , "item_id", "item_name", "value1", "value2" | 1 , 1 , "a" , 1L , 4L | 2 , 2 , "b" , 2L , 5L | 1 , 2 , "b" , 3L , 6L ) # columns to complete by col_terms <- qc(group, item_id, item_name) df %.>% complete_se(., col_terms)#> # A tibble: 8 x 5 #> group item_id item_name value1 value2 #> <dbl> <dbl> <chr> <int> <int> #> 1 1 1 a 1 4 #> 2 1 1 b NA NA #> 3 1 2 a NA NA #> 4 1 2 b 3 6 #> 5 2 1 a NA NA #> 6 2 1 b NA NA #> 7 2 2 a NA NA #> 8 2 2 b 2 5#> # A tibble: 8 x 5 #> group item_id item_name value1 value2 #> <dbl> <dbl> <chr> <dbl> <int> #> 1 1 1 a 1 4 #> 2 1 1 b 0 NA #> 3 1 2 a 0 NA #> 4 1 2 b 3 6 #> 5 2 1 a 0 NA #> 6 2 1 b 0 NA #> 7 2 2 a 0 NA #> 8 2 2 b 2 5# with nesting col_terms <- c("group", "tidyr::nesting(item_id, item_name)") df %.>% complete_se(., col_terms)#> # A tibble: 4 x 5 #> group item_id item_name value1 value2 #> <dbl> <dbl> <chr> <int> <int> #> 1 1 1 a 1 4 #> 2 1 2 b 3 6 #> 3 2 1 a NA NA #> 4 2 2 b 2 5#> # A tibble: 4 x 5 #> group item_id item_name value1 value2 #> <dbl> <dbl> <chr> <dbl> <int> #> 1 1 1 a 1 4 #> 2 1 2 b 3 6 #> 3 2 1 a 0 NA #> 4 2 2 b 2 5#> # A tibble: 4 x 5 #> group item_id item_name value1 value2 #> <dbl> <dbl> <chr> <dbl> <dbl> #> 1 1 1 a 1 4 #> 2 1 2 b 3 6 #> 3 2 1 a 0 0 #> 4 2 2 b 2 5