I\'m working with an imported data set that corresponds to the extract below:
set.seed(1)
dta <- data.frame(\"This is Column One\" = runif(n = 10),
mtcars %>%
data.table::setnames(
old = mtcars %>% names(),
new = mtcars %>% names() %>% paste0("_new_name")
)
The function setnames
in data.table
package is to rename the column names in data frame. old
and new
are two arguments in this function we need.
mtcars %>% names()
outputs the column names of data frame mtcars
in pipeline %>%
way, so you can also use names(mtcars)
. They are same thing.
In this minimal example, I rename the column names in pipeline %>%
and add all old column names with a postfix using paste0
function. You can add prefix, postfix or other rules.