knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(rmiscfun)

This function checks if a dataframe has all the columns from a reference vector of strings. It appends the missing columns and fills them out with a given value.

I created this function to be able to bind dataframes by rows and some of the columns are missing.

library(rmiscfun)

## This vector has two columns: a and b.
input_df <- data.frame(a = 1:3, b = letters[1:3])
input_df

## Reference vector
colnames_vector <- c("b", "c")

## Filler
filler <- -888

## Output vector
add_missing_columns(input_df, colnames_vector, filler)


## It does nothing if all the columns are present
add_missing_columns(input_df, c("b"), filler)


gbasulto/rmiscfun documentation built on July 25, 2019, 8:56 p.m.