Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to automatically name columns in R?

I have a dataset that currently looks like this:

   ColumnA ColumnB ColumnC
1      100     100     100
2      100     100     100
3      100     100     100
4      200     200     200
5      300     300     300
6      400     400     400
7      400     400     400
8      500     500     500
9      500     500     500
10     600     600     600
11     700     700     700

In the real dataset, there are 100 columns. I would like to rename them X1,X2,X3,X4,X5….X100. For example:

    X1  X2  X3
1  100 100 100
2  100 100 100
3  100 100 100
4  200 200 200
5  300 300 300
6  400 400 400
7  400 400 400
8  500 500 500
9  500 500 500
10 600 600 600
11 700 700 700

Is there a way to do this without having to manually change each of the 100 column names?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can assign the names with paste

names(df1) <- paste0("X", seq_along(df1))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading