Note: do not count on order of resulting data. Also only added rows are altered by the fill instructions.
replyr_coalesce( data, support, ..., fills = NULL, newRowColumn = NULL, copy = TRUE, tempNameGenerator = mk_tmp_name_source("replyr_coalesce") )
data | data.frame data to augment |
---|---|
support | data.frame rows of unique key-values into data |
... | not used, force later arguments to bind by name |
fills | list default values to fill in columns |
newRowColumn | character if not null name to use for new row indicator |
copy | logical if TRUE copy support to data's source |
tempNameGenerator | temp name generator produced by wrapr::mk_tmp_name_source, used to record dplyr::compute() effects. |
augmented data
# single column key example data <- data.frame(year = c(2005,2007,2010), count = c(6,1,NA), name = c('a','b','c'), stringsAsFactors = FALSE) support <- data.frame(year=2005:2010) filled <- replyr_coalesce(data, support, fills=list(count=0)) filled <- filled[order(filled$year), ] filled#> year count name #> 1 2005 6 a #> 4 2006 0 <NA> #> 2 2007 1 b #> 5 2008 0 <NA> #> 6 2009 0 <NA> #> 3 2010 NA c# complex key example data <- data.frame(year = c(2005,2007,2010), count = c(6,1,NA), name = c('a','b','c'), stringsAsFactors = FALSE) support <- expand.grid(year=2005:2010, name= c('a','b','c','d'), stringsAsFactors = FALSE) filled <- replyr_coalesce(data, support, fills=list(count=0)) filled <- filled[order(filled$year, filled$name), ] filled#> year count name #> 1 2005 6 a #> 9 2005 0 b #> 14 2005 0 c #> 19 2005 0 d #> 4 2006 0 a #> 10 2006 0 b #> 15 2006 0 c #> 20 2006 0 d #> 5 2007 0 a #> 2 2007 1 b #> 16 2007 0 c #> 21 2007 0 d #> 6 2008 0 a #> 11 2008 0 b #> 17 2008 0 c #> 22 2008 0 d #> 7 2009 0 a #> 12 2009 0 b #> 18 2009 0 c #> 23 2009 0 d #> 8 2010 0 a #> 13 2010 0 b #> 3 2010 NA c #> 24 2010 0 d