I would like to select columns if they are contained in a list. The problem is that I get an error for the columns that don't exist.
Try 1:
library(dplyr)
data(band_instruments)
desired_vars <- c("plays", "grade", "age")
is_desired_vars <- function(x) names(x) %in% desired_vars
band_instruments %>%
select_if(.predicate = is_desired_vars)
Try 2:
desired_vars <- c("plays", "grade", "age")
band_instruments[, desired_vars]
More Details:
I would like the solution to generalize. There may be a dataframe that this function acts on which would contain all columns in the list, but some dataframes will not contain all of the columns in this list.