1

I need to rename all columns in my data.frame. Right now, they are numbered 1-150 (without the X) but I would like to add "id" before each number.

Right now:

    c = data.frame(1, 2)
    names(c)[1] <- "1"
    names(c)[2] <- "2"

What I want: so that it is id1, id2 as each column name.

How can I do this?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Student
  • 73
  • 8

1 Answers1

2

You can use dplyr::rename_all()

library(dplyr)
iris %>% 
  rename_all(~ paste0("id_", .x)) %>% 
  names()

or with base R

setNames(
  iris, 
  nm = paste0(
    "id_", names(iris)
  )
) %>% names()

Colin FAY
  • 4,849
  • 1
  • 12
  • 29