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?
mydf
name
name1 name2 and name3 ..name-nth
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.
cols <- names(dat) == "name" names(dat)[cols] <- paste0("name", seq.int(sum(cols)))