I would like to use the names
function to apply the same column names to multiple dataframes, all of which have the same number of columns. I can of course do this the wrong way by calling names
for each dataframe, but I'd like to do it correctly. Here's the setup:
library(tidyverse)
df1 <- tibble(1,2,3,4,5)
df2 <- tibble(6,7,8,9,10)
df3 <- tibble(11,12,13,14,15)
df4 <- tibble(16,17,18,19,20)
column_names <- c("Alpha","Bravo","Charlie","Delta","Echo")
tibbles_list <- (c("df1","df2","df3","df4"))
The wrong way is of course:
names(df1) <- column_names
names(df2) <- column_names
names(df3) <- column_names
names(df4) <- column_names
I'd like to somehow use the list of dataframes in tibbles_list
(through as.name
or rlang::syms
or similar) to apply column_names
to all the dataframes in one line of code, perhaps using some species of purrr
's map
or one of the apply
functions in base R, but I'm completely at a loss as to how.