As you can see columns are having both numbers and characters. How can we rename these using dplyr?
Asked
Active
Viewed 39 times
0

Ketan Mehta
- 31
- 3
-
1Generally it can be done using `dplyr::rename` or `dplyr::rename_with`. – Andre Wildberg Jan 31 '23 at 23:35
-
Your column names look like years, so I think that you may want to get rid of the X in the names, However, column names are not permitted to start with a digit. see `help(make.names)` – G5W Jan 31 '23 at 23:45
-
1What do you want the new names to be? If you really want years as column names, and assuming you use something like `read.csv` to get the data, one option is the argument `read.csv(..., check.names = FALSE)`, which will preserve the years. This also looks like data you would want to reshape to long format with one "year" column. – neilfws Feb 01 '23 at 00:00
-
I really want to use dplyr and select columns starting with "X..and then rename them by removing X and preserving rest of the column name. – Ketan Mehta Feb 01 '23 at 00:15
-
(1) "year" in the column name violates a "soft" rule of mine: never have data (values) in a row or column name. It's "soft" because sometimes it just makes sense, but when I see something like this the first thing I tend to do is pivot/reshape the data from wide to long (see https://stackoverflow.com/q/2185252/3358272, https://stackoverflow.com/q/68058000/3358272). (2) Please do not post (only) an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). – r2evans Feb 01 '23 at 01:59