Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change column names with same name in dataframe

Tags:

r

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

like image 451
MAPK Avatar asked Jul 15 '15 03:07

MAPK


2 Answers

cols <- which(names(mydf == 'name'))
names(mydf)[cols] <- paste0('name', seq_along(cols))

The first line finds the indices of the columns with name 'name'. The second assigns new names.

like image 193
mathematical.coffee Avatar answered Sep 22 '22 20:09

mathematical.coffee


cols <- names(dat) == "name"
names(dat)[cols] <- paste0("name", seq.int(sum(cols)))
like image 39
Rorschach Avatar answered Sep 23 '22 20:09

Rorschach