name columns of multiple data.frames at once

hello, so if you have fore example 3 data.frames with 4 columns each and you want to name the 4 columns (same names) just at once for the 3 data.frames how could it be done ?

in my case i have 3 data.frames ab_PR7 , bt_PR7 and st_PR7

names(bt_PR7)[1] <- "variable"
names(bt_PR7)[2] <- "montant_06_2020"
names(bt_PR7)[3] <- "montant_06_2019"
names(bt_PR7)[4] <- "montant_12_2019"

names(ab_PR7)[1] <- "variable"
names(ab_PR7)[2] <- "montant_06_2020"
names(ab_PR7)[3] <- "montant_06_2019"
names(ab_PR7)[4] <- "montant_12_2019"

names(st_PR7)[1] <- "variable"
names(st_PR7)[2] <- "montant_06_2020"
names(st_PR7)[3] <- "montant_06_2019"
names(st_PR7)[4] <- "montant_12_2019"

how to do it just in once ...thanks in advance


library(purrr)

walk(
  c(
    "bt_PR7",
    "ab_PR7",
    "st_PR7"
  ),
  ~ {
    assign(., setNames(get(.), c(
      "variable",
      "montant_06_2020",
      "montant_06_2019",
      "montant_12_2019"
    )), envir = .GlobalEnv)
  }
)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.