#' Left join that fills missings in selected column with same column from right DF
#'
#' @param x left df
#' @param y right df
#' @param by by param (passed to left_join)
#' @param fill_col name of column to be filled from right df
#' @return left df, missings filled in based on right df
#' @import tidyverse
#' @export
#'
left_join_fill <- function(x, y, by, fill_col = "") {
if (!length(fill_col) == 1) {
warning("please only specify one column to be filled")
}
## DFs to tibble (in case their format is something else)
x <- as_tibble(x) %>%
ungroup()
y <- as_tibble(y) %>%
ungroup()
## Get nrow
nrow_start <- nrow(x)
## Declare columns to be filled
fcx <- paste0(fill_col, '.x')
fcy <- paste0(fill_col, '.y')
out <- left_join(x, y, by = by)
## Gen temp columns
out[, 'fcx_col'] <- out[, fcx]
out[, 'fcy_col'] <- out[, fcy]
## Join and fill
out <- out %>%
mutate(!!fill_col := coalesce(fcx_col, fcy_col)) %>%
dplyr::select(-fcx_col, -fcy_col, -all_of(c(fcx, fcy)))
## Get final n
nrow_end <- nrow(out)
if (!nrow_end == nrow_start) {
cat("Careful - merged dataset has different number of rows than original (left) data set.")
}
## Return
out
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.