12

I have multiple columns of different names in a data frame. For all of them i would like to add a common suffix

tot_proc

So, for example

DF
a   b   c

->

DF
a_tot_proc   b _tot_proc   c_tot_proc

I was able only to figure out how to add a prefix for column names:

colnames(DF) <- paste("tot_proc", colnames(DF), sep = "_")

but not suffix.Could you please help me. Thank you!

Anna Gorald
  • 131
  • 1
  • 1
  • 8

2 Answers2

17

You could just switch the order around.

colnames(DF) <- paste(colnames(DF), "tot_proc", sep = "_")
jmaval
  • 303
  • 2
  • 9
  • 1
    A handy generalized function: `appendDataFrameColumns<-function(df, prefix="", suffix="", sep="") { colnames(df) <- paste(prefix, colnames(df), suffix, sep=sep) return(df) }` – Gürol Canbek Sep 29 '18 at 15:25
2

With dplyr::rename_with

library(dplyr)
DF %>% rename_with(~ paste(., "tot_proc", sep = "_"))
Julien
  • 1,613
  • 1
  • 10
  • 26