Is there a clever way to use the rename function in dplyr when in some instances the column to be renamed doesn\'t exist?
For example, I would like the following not to
One more solution that can safely operate within dplyr without throwing an error using conditional evaluation {}. This will apply the rename if "foo" exists, but continue execution with the original df in the absence of a column named "foo".
mtcars %>%
{if("foo" %in% names(.)) rename(., missing_varible=foo) else .} %>%
rename(miles_per_gallon=mpg)