Add missing rows to data.table according to multiple keyed columns
A couple of possibilities are here - https://github.com/Rdatatable/data.table/pull/814
CJ.dt = function(...) {
rows = do.call(CJ, lapply(list(...), function(x) if(is.data.frame(x)) seq_len(nrow(x)) else seq_along(x)));
do.call(data.table, Map(function(x, y) x[y], list(...), rows))
}
setkey(mydata, name, job, sex, from)
mydata[CJ.dt(unique(data.table(name, job, sex)), unique(from))]
# name job sex from score
# 1: chris doctor male NYT 0.7383247
# 2: chris doctor male BG NA
# 3: chris doctor male TIME NA
# 4: chris doctor male USAT NA
# 5: chris lawyer female NYT NA
# 6: chris lawyer female BG -0.8204684
# 7: chris lawyer female TIME NA
# 8: chris lawyer female USAT NA
# 9: chris lawyer male NYT 0.4874291
#10: chris lawyer male BG NA
#11: chris lawyer male TIME NA
#12: chris lawyer male USAT NA
#13: john teacher male NYT -0.6264538
#14: john teacher male BG -0.8356286
#15: john teacher male TIME 1.5952808
#16: john teacher male USAT 0.1836433
#17: mary police female NYT NA
#18: mary police female BG NA
#19: mary police female TIME NA
#20: mary police female USAT 0.3295078
The dev version of tidyr now has an elegant way to do this because the expand()
function now supports nesting and crossing:
library(dplyr)
mydata <- data_frame(
name = c("john","john","john","john","mary","chris","chris","chris"),
job = c("teacher","teacher","teacher","teacher","police","lawyer","lawyer","doctor"),
sex = c("male","male","male","male","female","female","male","male"),
from = c("NYT","USAT","BG","TIME","USAT","BG","NYT","NYT"),
score = rnorm(8)
)
mydata %>%
expand(c(name, job, sex), from) %>%
left_join(mydata)
#> Joining by: c("name", "job", "sex", "from")
#> Source: local data frame [20 x 5]
#>
#> name job sex from score
#> 1 chris doctor male BG NA
#> 2 chris doctor male NYT 0.5448206
#> 3 chris doctor male TIME NA
#> 4 chris doctor male USAT NA
#> 5 chris lawyer female BG 1.2015173
#> 6 chris lawyer female NYT NA
#> 7 chris lawyer female TIME NA
#> 8 chris lawyer female USAT NA
#> 9 chris lawyer male BG NA
#> 10 chris lawyer male NYT -1.0930237
#> 11 chris lawyer male TIME NA
#> 12 chris lawyer male USAT NA
#> 13 john teacher male BG 1.1345461
#> 14 john teacher male NYT 1.3032946
#> 15 john teacher male TIME 2.4901830
#> 16 john teacher male USAT -1.6449096
#> 17 mary police female BG NA
#> 18 mary police female NYT NA
#> 19 mary police female TIME NA
#> 20 mary police female USAT -0.2443080