change column names with same name in dataframe

前端 未结 2 1622
我在风中等你
我在风中等你 2021-01-18 17:01

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

相关标签:
2条回答
  • 2021-01-18 17:14
    cols <- names(dat) == "name"
    names(dat)[cols] <- paste0("name", seq.int(sum(cols)))
    
    0 讨论(0)
  • 2021-01-18 17:24
    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.

    0 讨论(0)
提交回复
热议问题