How to change column names to capital letters from lower case or vice versa in R?


Mostly, we get data that contain column names in lowercase or just first letter is in upper case. If we want to convert those column names to all capital letter words or uppercase then toupper function can be used to the names of the columns. This can be done by using the below syntax −

Syntax

names(“data_frame_name”)<-toupper(names(“data_frame_name”))

Consider the below data frame −

Example

 Live Demo

set.seed(101)
Age<-sample(21:50,20)
Experience<-sample(0:10,20,replace=TRUE)
BP<-sample(60:140,20)
Employees<-data.frame(Age,Experience,BP)
Employees

Output

  Age Experience BP
1  32   7        95
2  22  10       140
3  40   2       120
4  38   7        85
5  27  10        90
6  28   8        77
7  35   0        65
8  45   4       127
9  34   4       116
10 50   7        67
11 47   4       134
12 42   3       136
13 39   2       106
14 36   1       109
15 43   5       129
16 29  10       111
17 41   2       104
18 23   8        93
19 25   0       103
20 21  10        96

Converting Age and Experience to AGE and EXPERIENCE −

Example

names(Employees)<-toupper(names(Employees)) Employees

Output

  AGE EXPERIENCE BP
1  29   4       140
2  45   9       103
3  34   9        85
4  43   10       68
5  37   4        62
6  42   9       133
7  23   4        79
8  44   3       125
9  50   7       124
10 47   7       104
11 41   3        87
12 22   5        81
13 46   7       130
14 48   6        89
15 32   9        84
16 21   9       128
17 33   8       139
18 26   9        77
19 28   8 106
20 30   4 109

Let’s have a look at another example −

Group1<-sample(0:20,20)
Group2<-sample(0:20,20)
Group3<-sample(0:20,20)
Group4<-sample(0:20,20)
Group5<-sample(0:20,20)
Group6<-sample(0:20,20)
Group7<-sample(0:20,20)
Group8<-sample(0:20,20)
Group9<-sample(0:20,20)
Group10<-sample(0:20,20)

Example

Student<-data.frame(Group1,Group2,Group3,Group4,Group5,Group6,Group7,Group8,Group9,Group10)
Student

Output

 Group1 Group2 Group3 Group4 Group5 Group6 Group7 Group8 Group9 Group10
1  16     3     13      6     12     8      10      20     7       20
2  11    20     15      14     6     18     0         0     11      16
3  1     18     3      18     13      20   6        11       6       0
4  8     8     11      15    19      9     12        14      5       10
5  9     11     2      5      0     11     15       4          4      3
6  4     4     17      0      4     13     13        18      8        19
7  13     12    19     13    10     7        14      19       17      6
8  18    1      4     10     14     4       7        10        13      2
9  17    6     10      3     15     5       1        17        14       17
10 14    17     6      7    11      19      20      3         16         18
11 15    9      1      2     18      3       3        9       10       14
12 2     15    18      11     3      10       9      5        19        9
13 12     7    9      17     16      2       19       8      20          8
14 19    10    16      8     5       1      17        1       12          5
15 5     5     0       4      8      1      5        5        16          3
16 3    0      8      20     20     6       2        13       9          11
17 0    14     14     12     17     14      4       12        15         1
18 4    18     20     2     20      1 7     16       8         7           1
19 7    13     7      9     2        12     18        6        15           7
20 6    16     5      16    9        0        16        15      2         13

Converting column names from Group1 to GROUP1 and so on for all columns −

Example

names(Student)<-toupper(names(Student))
Student

Output

GROUP1 GROUP2 GROUP3 GROUP4 GROUP5 GROUP6 GROUP7 GROUP8 GROUP9 GROUP10
1  16    3      13     6      12      8      10     20     7     20
2  11    20      15    14     6       18      0      0      11    16
3  1     18      3      18     13     20       6     11     6       0
4   8     8      11     15     19     9       12     14     5     10
5  9      11    2       5        0     11     15     4      4       3
6  4      4      17     0       4      13       13   18      8      19
7  13     12    19     13      10      7       14     19     17      6
8  18     1      4      10       14    4       7      10     13      2
9  17    6      10       3      15    5       1      17       14      17
10  14    17      6     7       11    19      20     3      16       18
11  15     9      1      2     18       3     3      9      10       14
12  2      15    18      11    3       10     9      5      19        9
13  12     7      9     17    16      2      19     8      20        8
14  19     10    16      8      5      1     17      1      12       5
15  5     5       0      4      8      15      5    16       3       1
16  3     0      8       20      20    6         2    13     9        11
17  0     14     14      12       17   14        4      12 7 15
18  4      18      20     2       20    1       7       16     8     7
19  7 13 7 9 2 12 18 6 15 7
20  6 16 5 16 9 0 16 15 2 13

Updated on: 16-Oct-2020

749 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements