How do I select columns that may or may not exist?
You can use any_of()
(from the tidyselect
package):
df %>% select(any_of(c("year", "boo")))
In the devel version of dplyr
df %>%
select(year, contains("boo"))
# year
#1 2000
#2 2001
#3 2002
#4 2003
#5 2004
#6 2005
#7 2006
#8 2007
#9 2008
#10 2009
#11 2010
gives the expected output
Otherwise one option would be to use one_of
df %>%
select(one_of("year", "boo"))
It returns a warning message if the column is not available
Other option is matches
df %>%
select(matches("year|boo"))
Here's a slight twist using dplyr::select_if()
that will not throw an Unknown columns:
warning if you try to select
a column name does not exist, in this case 'bad_column':
df %>%
select_if(names(.) %in% c('year', 'bar', 'bad_column'))