我有一个数据框中的多列具有不同的列名。对于所有这些列,我都希望添加一个共同的后缀。
tot_proc
比如说,
DF
a b c
->
表示 HTML 中的段落标签,用于在网页中显示一个段落。DF
a_tot_proc b _tot_proc c_tot_proc
我只能弄清如何为列名添加前缀:
colnames(DF) <- paste("tot_proc", colnames(DF), sep = "_")
但没有后缀。你能帮我一下吗?谢谢!
你只需要交换顺序。
colnames(DF) <- paste(colnames(DF), "tot_proc", sep = "_")
appendDataFrameColumns<-function(df, prefix="", suffix="", sep="") { colnames(df) <- paste(prefix, colnames(df), suffix, sep=sep) return(df) }
- Gürol Canbekdplyr::rename_with
library(dplyr)
DF %>% rename_with(~ paste(., "tot_proc", sep = "_"))
paste
函数中反转顺序:colnames(DF) <- paste("tot_proc", colnames(DF), sep = "_")
。 - nicola