Applying dplyr's rename to all columns while using pipe operator

前端 未结 5 1289

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),
               


        
5条回答
  •  执念已碎
    2021-01-04 03:34

    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.

提交回复
热议问题