How to add a new column to a data frame using mutate in R?

R ProgrammingServer Side ProgrammingProgramming

The mutate function of dplyr package in R can help us to add a new column to a data frame and the benefit of using mutate is that we can decide the position of the new column during the addition. For example, if we have a data frame called df that contains three columns say x, y, a then we can add a new column say z after y using mutate function. To understand how it can be done, check out the below examples.

Example1

Consider the below data frame −

Live Demo

> x1<-rpois(20,2)
> x3<-rpois(20,5)
> df1<-data.frame(x1,x3)
> df1

Output

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

Loading dplyr package and adding a new column x2 after x1 in df1 −

> library(dplyr)
> df1%>%mutate(x2=rpois(20,1),.after=x1)

Output

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

Example2

Live Demo

> y1<-rnorm(20)
> y2<-rnorm(20)
> y4<-rnorm(20)
> df2<-data.frame(y1,y2,y4)
> df2

Output

       y1           y2         y4
1   1.21750225  -1.33082010  0.7365231
2   0.24052365  -1.19657893 -0.7325931
3   0.82363925  -0.03890292 -0.3103461
4  -0.28315390  -0.20730053 -1.0066420
5  -0.56196875  -0.41592524  0.4996899
6   0.90983596   0.66105140 -0.9552844
7   1.14413650   1.20610045  0.9542341
8   0.03566065   1.31518070  0.2722798
9   1.72309925   0.55267260  0.3036829
10  1.27783338  -0.61818175 -0.2573076
11  0.22074289   1.23057901  0.5180043
12  1.60663571  -1.00737269  1.1614623
13 -0.75813279  -0.36594209  0.3923075
14 -0.31492265   1.30409915 -0.2759040
15 -1.03101619   0.15687986  0.8609099
16 -0.37968676  -0.04247421 -0.7490176
17 -1.90078740  -0.61468534  1.0015994
18 -0.76753148   0.21451207 -0.1875631
19 -0.36281597  -0.94474847 -1.1014309
20 -1.90049600  -0.20750306  2.1602226

adding a new column y3 after y2 in df2 −

> df2%>%mutate(y3=rnorm(20,10,0.5),.after=y2)

Output

      y1           y2          y3         y4
1   1.21750225  -1.33082010  8.801898   0.7365231
2   0.24052365  -1.19657893  9.592606  -0.7325931
3   0.82363925  -0.03890292  10.088155 -0.3103461
4  -0.28315390  -0.20730053  9.297835  -1.0066420
5  -0.56196875  -0.41592524  9.463427   0.4996899
6   0.90983596   0.66105140  9.494079  -0.9552844
7   1.14413650   1.20610045  9.689292   0.9542341
8   0.03566065   1.31518070  10.664904  0.2722798
9   1.72309925   0.55267260  10.778179  0.3036829
10  1.27783338  -0.61818175  9.872532  -0.2573076
11  0.22074289   1.23057901  9.301869   0.5180043
12  1.60663571  -1.00737269  10.176794  1.1614623
13 -0.75813279  -0.36594209  9.367227   0.3923075
14 -0.31492265   1.30409915  11.086966 -0.2759040
15 -1.03101619   0.15687986  9.082294   0.8609099
16 -0.37968676  -0.04247421  9.547622  -0.7490176
17 -1.90078740  -0.61468534  9.694575   1.0015994
18 -0.76753148   0.21451207  10.583576 -0.1875631
19 -0.36281597  -0.94474847  10.075352 -1.1014309
20 -1.90049600  -0.20750306  10.106721 2.1602226
raja
Published on 04-Mar-2021 08:16:32
Advertisements