Skip to contents

Small helper function. Checks if all expected columns are in the data frame and if not, adds a column with all missing variables in the data. Thus, the columns will be made explicitly missing in the data frame.

Usage

add_missing_columns(data, columns)

Arguments

data

A data frame.

columns

Expected columns in the data frame.

Value

A data frame with at least all the columns named in columns. The added columns will be of class character.

Examples

add_missing_columns(head(iris), c("important_column1", "important_column2"))
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species important_column1
#> 1          5.1         3.5          1.4         0.2  setosa              <NA>
#> 2          4.9         3.0          1.4         0.2  setosa              <NA>
#> 3          4.7         3.2          1.3         0.2  setosa              <NA>
#> 4          4.6         3.1          1.5         0.2  setosa              <NA>
#> 5          5.0         3.6          1.4         0.2  setosa              <NA>
#> 6          5.4         3.9          1.7         0.4  setosa              <NA>
#>   important_column2
#> 1              <NA>
#> 2              <NA>
#> 3              <NA>
#> 4              <NA>
#> 5              <NA>
#> 6              <NA>