Turns implicit missing values into explicit missing values. All the combinations of column values (should be unique) will be constructed. Other columns will be filled with NAs or constant value.
complete_dt(.data, ..., fill = NA)
data.frame
Specification of columns to expand.The selection of columns is
supported by the flexible select_dt
.
To find all unique combinations of provided columns, including those not found in the data,
supply each variable as a separate argument. But the two modes (select the
needed columns and fill outside values) could not be mixed,
find more details in examples.
Atomic value to fill into the missing cell, default uses NA
.
data.table
When the provided columns with addtion data are of different length, all the unique combinations would be returned. This operation should be used only on unique entries, and it will always returned the unique entries.
If you supply fill parameter, these values will also replace existing explicit missing values in the data set.
df <- data.table(
group = c(1:2, 1),
item_id = c(1:2, 2),
item_name = c("a", "b", "b"),
value1 = 1:3,
value2 = 4:6
)
df %>% complete_dt(item_id,item_name)
#> Key: <item_id, item_name>
#> item_id item_name group value1 value2
#> <num> <char> <num> <int> <int>
#> 1: 1 a 1 1 4
#> 2: 1 b NA NA NA
#> 3: 2 a NA NA NA
#> 4: 2 b 2 2 5
#> 5: 2 b 1 3 6
df %>% complete_dt(item_id,item_name,fill = 0)
#> Key: <item_id, item_name>
#> item_id item_name group value1 value2
#> <num> <char> <num> <int> <int>
#> 1: 1 a 1 1 4
#> 2: 1 b 0 0 0
#> 3: 2 a 0 0 0
#> 4: 2 b 2 2 5
#> 5: 2 b 1 3 6
df %>% complete_dt("item")
#> Key: <item_id, item_name>
#> item_id item_name group value1 value2
#> <num> <char> <num> <int> <int>
#> 1: 1 a 1 1 4
#> 2: 1 b NA NA NA
#> 3: 2 a NA NA NA
#> 4: 2 b 2 2 5
#> 5: 2 b 1 3 6
df %>% complete_dt(item_id=1:3)
#> Key: <item_id>
#> item_id group item_name value1 value2
#> <int> <num> <char> <int> <int>
#> 1: 1 1 a 1 4
#> 2: 2 2 b 2 5
#> 3: 2 1 b 3 6
#> 4: 3 NA <NA> NA NA
df %>% complete_dt(item_id=1:3,group=1:2)
#> Key: <item_id, group>
#> item_id group item_name value1 value2
#> <int> <int> <char> <int> <int>
#> 1: 1 1 a 1 4
#> 2: 1 2 <NA> NA NA
#> 3: 2 1 b 3 6
#> 4: 2 2 b 2 5
#> 5: 3 1 <NA> NA NA
#> 6: 3 2 <NA> NA NA
df %>% complete_dt(item_id=1:3,group=1:3,item_name=c("a","b","c"))
#> Key: <item_id, group, item_name>
#> item_id group item_name value1 value2
#> <int> <int> <char> <int> <int>
#> 1: 1 1 a 1 4
#> 2: 1 1 b NA NA
#> 3: 1 1 c NA NA
#> 4: 1 2 a NA NA
#> 5: 1 2 b NA NA
#> 6: 1 2 c NA NA
#> 7: 1 3 a NA NA
#> 8: 1 3 b NA NA
#> 9: 1 3 c NA NA
#> 10: 2 1 a NA NA
#> 11: 2 1 b 3 6
#> 12: 2 1 c NA NA
#> 13: 2 2 a NA NA
#> 14: 2 2 b 2 5
#> 15: 2 2 c NA NA
#> 16: 2 3 a NA NA
#> 17: 2 3 b NA NA
#> 18: 2 3 c NA NA
#> 19: 3 1 a NA NA
#> 20: 3 1 b NA NA
#> 21: 3 1 c NA NA
#> 22: 3 2 a NA NA
#> 23: 3 2 b NA NA
#> 24: 3 2 c NA NA
#> 25: 3 3 a NA NA
#> 26: 3 3 b NA NA
#> 27: 3 3 c NA NA
#> item_id group item_name value1 value2