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?
CodePudding user response:
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))
CodePudding user response:
A tidyverse solution: rename_with()
require(dplyr)
## '~' notation can be used for formulae in this context:
df <- rename_with(df, ~ paste0("Cluster_", .))