How to divide all columns by one column and keeping original data in R?


To divide all columns of data frame in R by one column and keeping the original data, we can use mutate_at function of dplyr package along with list function.

For example, if we have a data frame called df that contains five columns say x, y, z, a, and b then we can divide all columns by b and keep the original data by using the below given command −

df%>%mutate_at(vars(x:b),list(All_by_b=~./b))

Example 1

Following snippet creates a sample data frame −

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

The following dataframe is created −

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

In order to load dplyr package and divide each column by x3, add the following code to the above snippet −

library(dplyr)
df1%>%mutate_at(vars(x1:x3),list(All_by_x3=~./x3))

Output

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

   x1 x2 x3 x1_All_by_x3 x2_All_by_x3 x3_All_by_x3
1  6  8  5  1.200000    1.600000       1
2  3  5  2  1.500000    2.500000       1
3  5  4  4  1.250000    1.000000       1
4  5  4  5  1.000000    0.800000       1
5  6  5  3  2.000000    1.666667       1
6  3  6  2  1.500000    3.000000       1
7 10  6  1 10.000000    6.000000       1
8  3  4  1  3.000000    4.000000       1
9  2  6  2  1.000000    3.000000       1
10 4  3  2  2.000000    1.500000       1
11 4  7  1  4.000000    7.000000       1
12 3  8  5  0.600000    1.600000       1
13 6  9  3  2.000000    3.000000       1
14 3  5  2  1.500000    2.500000       1
15 3  5  5  0.600000    1.000000       1
16 3  6  1  3.000000    6.000000       1
17 5  1  2  2.500000    0.500000       1
18 6  7  1  6.000000    7.000000       1
19 4  5  1  4.000000    5.000000       1
20 5  3  3  1.666667    1.000000       1

Example 2

Following snippet creates a sample data frame −

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

The following dataframe is created −

        y1          y2
1   0.14773302   1.16132215
2  -0.32074626  -1.49244515
3   2.19398775   0.28150318
4   1.01693094  -1.40535898
5  -1.42448609   1.67779412
6  -0.15938972  -1.09495586
7   0.52470550  -0.99145523
8  -0.78363853  -0.06675766
9   0.07709268  -0.12294655
10  0.78635469   2.58087152
11 -0.41811848  -0.41705329
12  0.64354838  -0.89271271
13 -0.42895143   0.52703245
14 -0.56728461  -0.43831806
15  0.39307336   0.08188360
16  1.10202078   0.35529659
17 -1.71921960   1.33118820
18 -1.13380901  -1.55504071
19  1.16703557   1.24537827
20 -0.32852345   1.48512287

To divide each column by y1, add the following code to the above snippet −

df2%>%mutate_at(vars(y1:y2),list(All_by_y1=~./y1))

Output

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

      y1           y2      y1_All_by_y1  y2_All_by_y1
1   0.14773302   1.16132215   1         7.86095182
2  -0.32074626  -1.49244515   1         4.65303993
3   2.19398775   0.28150318   1         0.12830663
4   1.01693094  -1.40535898   1        -1.38196108
5  -1.42448609   1.67779412   1        -1.17782416
6  -0.15938972  -1.09495586   1         6.86967673
7   0.52470550  -0.99145523   1        -1.88954609
8  -0.78363853  -0.06675766   1         0.08518935
9   0.07709268  -0.12294655   1        -1.59478895
10  0.78635469   2.58087152   1         3.28207048
11 -0.41811848  -0.41705329   1         0.99745242
12  0.64354838  -0.89271271   1        -1.38717265
13 -0.42895143   0.52703245   1        -1.22865298
14 -0.56728461  -0.43831806   1         0.77265989
15  0.39307336   0.08188360   1         0.20831633
16  1.10202078   0.35529659   1         0.32240462
17 -1.71921960   1.33118820   1        -0.77429794
18 -1.13380901  -1.55504071   1         1.37151910
19  1.16703557   1.24537827   1         1.06712966
20 -0.32852345   1.48512287   1        -4.52059930

Updated on: 05-Nov-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements