I have a batch of data sets with many variables. I need to rename some variables in each data set to a common name, without changing the others. One issue is that each the column I want to rename is in a different location in each data set and each data set has a different number of columns. Example data and code below.
Working Data:
package(tidyverse)
df1 <- tribble(
~var1, ~var2, ~var3, ~var4, ~var5,
"1", "1", "1", "a", "d",
"2", "2", "2", "b", "e",
"3", "3", "3", "c", "f"
)
df2 <- tribble(
~var1, ~help, ~var3,
"1", "1", "1",
"2", "2", "2",
"3", "3", "3"
)
df3 <- tribble(
~var1, ~newCol, ~var3, ~help, ~var5, ~var6,
"1", "4", "1", "a", "d", "1",
"2", "5", "2", "b", "e", "2",
"3", "6", "3", "c", "f", "3"
)
I would like to recode help
to var2
so it is common across the data sets. I know that I can do it individually using dplyr
like this:
df2 <- df2 %>%
rename(var2 = help)
df3 <- df3 %>%
rename(var2 = help)
But I have to do this across over 100 data sets, and I would like to do it more efficiently. I was using the suggested answer from this post as an example, but it only works for data sets that have the same number and order of columns, which mine do not. I did try this, but it didn't work:
dfs <- list(df2$help = df2$help, df3$help = df3$help)
colnames <- c("var2", "var2")
list2env(lapply(dfs, setNames, colnames), .GlobalEnv)
But I get the following error at the dfs <- list(df2$help = df2$help
... command:
Error: unexpected '=' in "dfs <- list(df2$help ="
All of the examples I could find do not show how to change specific columns across data sets, only how to change all column names or change a small number of columns in data sets that only have a few columns.