Change column names with same name in dataframe to R

I have a dataframe mydfwith n number of columns with the same say column name name. I want to change them to columns name1 name2 and name3 ..name-nth. How to do it in R?

+4
source share
2 answers
cols <- which(names(mydf == 'name'))
names(mydf)[cols] <- paste0('name', seq_along(cols))

The first row finds column indices named "name". The second gives new names.

+6
source
cols <- names(dat) == "name"
names(dat)[cols] <- paste0("name", seq.int(sum(cols)))
+4
source

All Articles