简体   繁体   中英

Rename column names of a dataframe with incrementation

I have a script generating a dataframe with multiple columns named with numbers 1, 2, 3 –> n

I want to rename the columns with the following names: "Cluster_1", "Cluster_2", "Cluster_3" –> "Cluster_n" (with incrementation).

As the number of columns in my dataframe can change accordingly to another part of my script, I would like to be able to have a kind of loop structure that would go through my dataframe and change columns accordingly.

I would like to do something like:

for (i in colnames(df)){
    an expression that would change the column name to a concatenation of "Cluster_" + i
}

Outside the loop context, I generally use this expression to rename a column:

names(df)[names(df) == '1'] <- 'Cluster_1'

But I struggle to produce an adapted version of this expression that would properly integrate in my for loop with a concatenation of string and variable value.

How can I adjust the expression that renames the column of the dataframe to integrate in my for loop?

Or is there a better way than a for loop to do this?

A tidyverse solution: rename_with()

require(dplyr)

## '~' notation can be used for formulae in this context:
df <- rename_with(df, ~ paste0("Cluster_", .))

Using paste0 .

names(df) <- paste0('cluster_', seq_len(length(df)))

If you really need a for loop, try

for (i in seq_along(names(df))) {
  names(df)[i] <- paste0('cluster_', i)
}

df
#   cluster_1 cluster_2 cluster_3 cluster_4
# 1         1         4         7        10
# 2         2         5         8        11
# 3         3         6         9        12

Note: colnames()/rownames() is designed for class "matrix" , for "data.frame" s, you might want to use names()/row.names() .


Data:

df <- data.frame(matrix(1:12, 3, 4))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM