0

I learned via this post (Adding a prefix to column names) how to change the names of all the variables in a dataset at once, but I can't seem to change the name of only a subset of those variables. The dataset has 358 variables in total, and I am able to add a prefix to all of the variables, but not just a subset.

I am using the following code, but the names don't seem to be changing.

colnames(y6t1data[,75:358]) <- paste("Y6T1",colnames(y6t1data[,75:358]), sep = "_")
Benji
  • 79
  • 1
  • 9

1 Answers1

0

If you are using data.table framework, an intuitive way to do that is:

setnames(y6t1data, 75:358, paste("Y6T1",names(y6t1data)[75:358], sep = "_")

This syntax alows you to transform names, by regex...

For more info, pleace check:

? setnames
Emmanuel-Lin
  • 1,690
  • 1
  • 12
  • 29