How to add one column in an R data frame to rest of the next columns?



Data Analysis projects might require things that we generally think simple but they are actually helpful to achieve objectives, one such thing would be adding values or columns of an R data frame. To add one column to rest of the next columns, we can replace the next columns by adding the one column by using single square brackets.

Consider the below data frame −

Example

 Live Demo

x1<-rnorm(20) x2<-rnorm(20) x3<-rnorm(20) x4<-rnorm(20) df1<-data.frame(x1,x2,x3,x4) df1

Output

       x1          x2             x3             x4 1   1.85664999   -0.9065488762   0.293178644    0.55457359 2   0.44698508    0.9893899854   0.536684098    0.28302308 3   0.61888012   -0.4300288618   1.030952802   -0.25713481 4  -0.05365888    1.1227376933   1.382627238    2.00689788 5   0.03964954    2.2557976594  -0.902402177   -1.58164559 6  -0.66872443   -0.0004653294   2.125212420   -1.10464733 7   0.03000691   -0.7238778598  -0.669792673    0.30548849 8  -0.43260468    0.4872185286   0.197004732   -1.84113665 9  -0.95276898   -1.0384948791  -0.007177681   -1.59672437 10 -0.83272853    0.5075483877   0.585314160    0.72559385 11 -0.23842261    0.2950442950   0.593196270   -0.11921104 12 -0.17180004   -0.0105962399   0.927656280   -1.07630231 13  0.26177947   -0.2539494068   0.333217120    0.62433826 14  0.42988050   -0.5740571129  -0.727895313   -0.58763643 15  0.47914315   -0.3317974395  -1.502326687   -0.12933857 16  0.72317202    0.4056645753   0.720629400    1.51496596 17 -0.63108679    1.1358016229  -0.467778438    0.40033389 18 -0.39403524   -1.3937678989  -1.579988055    1.61907436 19  0.38662052   -0.2191345379   0.409531230   -2.50333812 20 -0.34579323    0.2170208774   0.441002799    0.08652698

Adding column 1 to columns 2 to 4 in df1 −

Example

df1[2:ncol(df1)]<-df1[2:ncol(df1)]+df1[,1] df1

Output

        x1            x2            x3          x4 1   1.85664999    0.950101112    2.14982863    2.4112236 2   0.44698508    1.436375070    0.98366918    0.7300082 3   0.61888012    0.188851261    1.64983293    0.3617453 4  -0.05365888    1.069078817    1.32896836    1.9532390 5   0.03964954    2.295447201   -0.86275264   -1.5419960 6  -0.66872443   -0.669189755    1.45648799   -1.7733718 7   0.03000691   -0.693870954   -0.63978577    0.3354954 8  -0.43260468    0.054613849   -0.23559995   -2.2737413 9  -0.95276898   -1.991263856   -0.95994666   -2.5494933 10 -0.83272853   -0.325180141   -0.24741437   -0.1071347 11 -0.23842261    0.056621690    0.35477366   -0.3576336 12 -0.17180004   -0.182396283    0.75585624   -1.2481024 13  0.26177947    0.007830061    0.59499659    0.8861177 14  0.42988050   -0.144176615   -0.29801481   -0.1577559 15  0.47914315    0.147345709   -1.02318354    0.3498046 16  0.72317202    1.128836595    1.44380142    2.2381380 17 -0.63108679    0.504714834   -1.09886523   -0.2307529 18 -0.39403524   -1.787803135   -1.97402329    1.2250391 19  0.38662052    0.167485982    0.79615175   -2.1167176 20 -0.34579323   -0.128772348    0.09520957   -0.2592662

Example

 Live Demo

y1<-rpois(20,5) y2<-rpois(20,5) y3<-rpois(20,5) y4<-rpois(20,5) y5<-rpois(20,5) df2<-data.frame(y1,y2,y3,y4,y5) df2

Output

   y1 y2 y3   y4 y5 1  11  5   3    2   9 2  7   4   6   11   3 3  2   4   3    5   5 4  3   4   3    8   5 5  7   5   6    6   3 6  3   4   5    6   8 7  4   6   5    7   4 8  6   4   6    5   3 9  6   2   4    8   8 10 7   8   3    3   4 11 2  10   01   2   5 12 7   7   2    6   1 13 8   5   6    3   2 14 5   5   4    9   4 15 5   5   6    5   7 16 6   4   8    9   4 17 3   3   5    9   0 18 5   4   3    5   2 19 5   6   9   11   4 20 4   4   3    4  10

Adding column 1 to columns 2 to 5 in df2 −

Example

df2[2:ncol(df2)]<-df2[2:ncol(df2)]+df2[,1] df2

Output

   y1  y2   y3   y4   y5 1  11  16   14   13    20 2  7   11   13  18     10 3  2   6    5    7     7 4  3   7    6   11     8 5  7   12   13  13    10 6  3   7    8   9     11 7  4   10   9   11    8 8  6   10   12  11    9 9  6   8   10   14    14 10 7   15   10  10    11 11 2   12   3   4     7 12 7   14   9   13     8 13 8   13   14  11   10 14 5   10   9   14     9 15 5   10   11  10    12 16 6   10   14  15    10 17 3   6    8   12    3 18 5   9    8   10    7 19 5   11   14  16    9 20 4   8    7    8    14
Updated on: 2021-02-10T05:20:45+05:30

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements