How to change the name of single column using setNames in R?

R ProgrammingServer Side ProgrammingProgramming

To change the name of single column using setNames, we would need to specify the column name that needs to be changed.

For example, if we have a data frame called df that contains three columns say Var1, var2, and Var3 and we want to change var2 to Var2 then we can use the command as follows −

setNames(df,replace(names(df),names(df)=="var2","Var2"))

Example 1

Following snippet creates a sample data frame −

x<-rnorm(20)
x1<-rnorm(20)
df1<-data.frame(x,x1)
df1

Output

The following dataframe is created −

       x            x1
1   0.885142314  -0.18025261
2  -0.048954839   0.87720112
3   0.896212666  -0.53714354
4   0.539095496  -0.19380810
5  -1.706763224   0.79608177
6  -1.628122107  -1.17570164
7  -1.000949495   0.09243436
8   1.344899293  -0.34504828
9   0.290659812   0.57493564
10 -1.366119792   0.39442545
11 -0.002044629  -0.60124159
12  1.056147083  -0.05670359
13  1.245766258  -0.91321178
14  1.202689649   0.42440362
15 -1.238028330  -1.31709063
16  0.962875354   1.43057923
17  0.692604922  -0.60277152
18  1.721352577   0.67629134
19 -2.373829503  -0.09540660
20  0.031280271   1.90790870

In order to change the name of column x1 to y, add the following code to the above snippet −

x<-rnorm(20)
x1<-rnorm(20)
df1<-data.frame(x,x1)
setNames(df1,replace(names(df1),names(df1)=="x1","y"))

Output

If you execute all the above given codes as a single program, it generates the following output −

         x           y
1   0.885142314  -0.18025261
2  -0.048954839   0.87720112
3   0.896212666  -0.53714354
4   0.539095496  -0.19380810
5  -1.706763224   0.79608177
6  -1.628122107  -1.17570164
7  -1.000949495   0.09243436
8   1.344899293  -0.34504828
9   0.290659812   0.57493564
10 -1.366119792   0.39442545
11 -0.002044629  -0.60124159
12  1.056147083  -0.05670359
13  1.245766258  -0.91321178
14  1.202689649   0.42440362
15 -1.238028330  -1.31709063
16  0.962875354   1.43057923
17  0.692604922  -0.60277152
18  1.721352577   0.67629134
19 -2.373829503  -0.09540660
20  0.031280271   1.90790870

Example 2

Following snippet creates a sample data frame −

a<-rpois(20,5)
a_2<-rpois(20,2)
df2<-data.frame(a,a_2)
df2

Output

The following dataframe is created −

   a a_2
1  5  7
2  3  2
3  7  2
4  5  0
5  6  0
6  4  1
7  8  3
8  6  1
9  7  2
10 6  3
11 4  0
12 3  2
13 5  0
14 7  2
15 3  1
16 9  3
17 2  2
18 4  1
19 2  7
20 1  1

In order to change the name of column a_2 to b, add the following code to the above snippet −

a<-rpois(20,5)
a_2<-rpois(20,2)
df2<-data.frame(a,a_2)
df2<-data.frame(a,a_2) 
setNames(df2,replace(names(df2),names(df2)=="a_2","b"))

Output

If you execute all the above given codes as a single program, it generates the following output −

   a  b
1  5  7
2  3  2
3  7  2
4  5  0
5  6  0
6  4  1
7  8  3
8  6  1
9  7  2
10 6  3
11 4  0
12 3  2
13 5  0
14 7  2
15 3  1
16 9  3
17 2  2
18 4  1
19 2  7
20 1  1
raja
Updated on 06-Nov-2021 07:43:22

Advertisements