I have the following list of data frames containing a column named cyl
mt_list <- list(head(mtcars[, 1:2]), tail(mtcars[, 1:2]), mtcars[13:18, 1:2])
mt_list
new_cyl_names <- c("cyl1", "cyl2", "cyl3")
new_cyl_names
I would like to name the cyl
corresponding value in the character vector new_cyl_names
.
I tried to do it as follows:
change_colname_cyl <- function(df, new_colname){
df %>%
dplyr::rename(new_colname = cyl)
}
purrr::map2(.x = mt_list, .y = new_cyl_names, ~ change_colname_cyl(.x, .y))
This results in (only the first data frame shown):
[[1]]
mpg new_colname
Mazda RX4 21.0 6
Mazda RX4 Wag 21.0 6
Datsun 710 22.8 4
Hornet 4 Drive 21.4 6
Hornet Sportabout 18.7 8
Valiant 18.1 6
Can someone help me use purrr
this correctly , i.e. change cyl
to cyl1
in this case instead of new_colname
to above?
source
share