2

My question is about renaming multiple column names at once.

I have a dataframe called 'growth' with 46 columns.

Columns 2:46 are all named as dates, but all of the dates have an X in front of them, e.g. 'X1981'.

Naturally I want to remove the X from all of the column names.

I cannot understand why the following is not working:

colnames(growth[ ,2:length(growth)]) <- substring(colnames(growth[ ,2:length(growth)]),2)

Please help me with some insights.

Wade Byron Profe
  • 135
  • 2
  • 2
  • 18
  • 1
    Probably related to [this](https://stackoverflow.com/questions/23427925/difference-between-namesdf1-and-namesdf1) – David Arenburg Jun 24 '18 at 11:49
  • You actually should reshape your wide data to long format, having a *Year* column and values in adjacent column. This scales better and computation is much easier. – Parfait Jun 24 '18 at 13:03
  • Possible duplicate of [Difference between \`names(df\[1\]) – Rui Barradas Jun 24 '18 at 14:12

1 Answers1

2

Nevermind, I changed the instruction to...

names(growth)[2:46] <- substring(names(growth)[2:46],2)

...and now it works. Clearly it had something to do with how I was subsetting the columns.

Wade Byron Profe
  • 135
  • 2
  • 2
  • 18