I have a dataset that I need to dynamically rename specific columns.
For example, I would normally rename the "mtcars" dataset the following way:
# THIS WORKS
# Load data
data <- mtcars
# Gather data.frame with columns to rename, I am doing it this way because I generally have
# a long list of "configurations" to plot that I go through in a for loop. Might not be the most
# efficient, but it generally works for me.
Columns_to_rename <- data.table(X = "mpg", Y = "gear", Color = "carb")
# Rename columns
plot_data <- data %>%
dplyr::rename(X := !!Columns_to_rename$X[1],
Y := !!Columns_to_rename$Y[1],
Color := !!Columns_to_rename$Color[1]) %>%
dplyr::select(X, Y, Color)
However, in my real dataset with the real Columns_to_rename, my dataset may not contain a header. I would just want to ignore that renaming.
Consider the case in the above example:
Columns_to_rename <- data.table(X = "mpg", Y = "gear", Color = "missing")
As far as I am aware, the dplyr package requires all columns to exist. Sticking with the tidyverse, I looked into using plyr package because I know this allows for missing column names. Unfortunately I could not figure out how to also call the columns dynamically. I have looked through previous StackOverflow questions and Answers, but haven't seen anyone needing to combine these two principles in their renaming.
Thanks!