How to divide each value in a data frame by column total in R?

R ProgrammingServer Side ProgrammingProgramming

To divide each value in a data frame by column total, we can use apply function and define the function for the division. For example, if we have a data frame called df that contains five columns then we can divide each value of these columns by column total using the command apply(df,2,function(x){x/sum(x)})

Example

Consider the below data frame −

 Live Demo

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

Output

   x1 x2  x3
1  4  3   9
2  9  5   8
3  4  2   6
4  8  3   9
5  3  2  10
6  8  0   8
7  3  3   7
8  6  2  10
9  9  1   8
10 3  2   3
11 4  2   7
12 4  3   6
13 1  3  11
14 6  2   7
15 5  2  10
16 5  2  11
17 3  0  13
18 4  0   7
19 4  2  15
20 4  3  10
21 6  3   9
22 5  3   2
23 7  3   6
24 3  5   9
25 1  7   8
26 1  3   7
27 4  2   7
28 4  1   5
29 3  1   8
30 8  1  10
31 7  1   8
32 5  0   5
33 3  3   3
34 2  2   9
35 5  2   4
36 6  4   9
37 3  1   8
38 7  4  11
39 5  2   5
40 6  0  11

Dividing each value by column total in df1 −

Example

apply(df1,2,function(x){x/sum(x)})

Output

           x1          x2         x3
[1,]  0.021276596  0.03333333  0.028213166
[2,]  0.047872340  0.05555556  0.025078370
[3,]  0.021276596  0.02222222  0.018808777
[4,]  0.042553191  0.03333333  0.028213166
[5,]  0.015957447  0.02222222  0.031347962
[6,]  0.042553191  0.00000000  0.025078370
[7,]  0.015957447  0.03333333  0.021943574
[8,]  0.031914894  0.02222222  0.031347962
[9,]  0.047872340  0.01111111  0.025078370
[10,] 0.015957447  0.02222222  0.009404389
[11,] 0.021276596  0.02222222  0.021943574
[12,] 0.021276596  0.03333333  0.018808777
[13,] 0.005319149  0.03333333  0.034482759
[14,] 0.031914894  0.02222222  0.021943574
[15,] 0.026595745  0.02222222  0.031347962
[16,] 0.026595745  0.02222222  0.034482759
[17,] 0.015957447  0.00000000  0.040752351
[18,] 0.021276596  0.00000000  0.021943574
[19,] 0.021276596  0.02222222  0.047021944
[20,] 0.021276596  0.03333333  0.031347962
[21,] 0.031914894  0.03333333  0.028213166
[22,] 0.026595745  0.03333333  0.006269592
[23,] 0.037234043  0.03333333  0.018808777
[24,] 0.015957447  0.05555556  0.028213166
[25,] 0.005319149  0.07777778  0.025078370
[26,] 0.005319149  0.03333333  0.021943574
[27,] 0.021276596  0.02222222  0.021943574
[28,] 0.021276596  0.01111111  0.015673981
[29,] 0.015957447  0.01111111  0.025078370
[30,] 0.042553191  0.01111111  0.031347962
[31,] 0.037234043  0.01111111  0.025078370
[32,] 0.026595745  0.00000000  0.015673981
[33,] 0.015957447  0.03333333  0.009404389
[34,] 0.010638298  0.02222222  0.028213166
[35,] 0.026595745  0.02222222  0.012539185
[36,] 0.031914894  0.04444444  0.028213166
[37,] 0.015957447  0.01111111  0.025078370
[38,] 0.037234043  0.04444444  0.034482759
[39,] 0.026595745  0.02222222  0.015673981
[40,] 0.031914894  0.00000000  0.034482759

Example

 Live Demo

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

Output

       y1           y2
1   1.52398233   0.423204080
2   0.48580249  -0.902605575
3   2.67630858   0.007436699
4   0.68410093   0.147904838
5  -1.40680934   1.223015890
6  -2.58064644   1.573868810
7  -0.82872756  -1.663446039
8   0.62632080  -0.478541658
9  -0.52795034  -0.236118274
10  1.68900397  -1.692100343
11 -1.37090356  -0.693266887
12  0.27027656   1.206031106
13 -1.40924870   2.025970432
14  1.06878458  -0.421947510
15 -1.18499758   0.152058450
16  0.85252095   0.520451585
17  0.33823672   0.003101624
18 -0.01792139   0.160589590
19  0.50504901  -0.370946479
20  0.37809177  -1.276916712

Dividing each value by column total in df2 −

Example

apply(df2,2,function(x){x/sum(x)})

Output

         y1            y2
[1,]   0.8603878  -1.44805767
[2,]   0.2742673   3.08840341
[3,]   1.5109514  -0.02544581
[4,]   0.3862198  -0.50607909
[5,]  -0.7942360  -4.18473645
[6,]  -1.4569439  -5.38523353
[7,]  -0.4678710   5.69173576
[8,]   0.3535991   1.63740368
[9,]  -0.2980625   0.80791489
[10,]  0.9535533   5.78978085
[11,] -0.7739648   2.37211898
[12,]  0.1525888  -4.12662041
[13,] -0.7956132  -6.93216857
[14,]  0.6033989   1.44375812
[15,] -0.6690087  -0.52029131
[16,]  0.4813039  -1.78080492
[17,]  0.1909568  -0.01061268
[18,] -0.0101178  -0.54948191
[19,]  0.2851332   1.26925027
[20,]  0.2134576   4.36916638
raja
Published on 17-Mar-2021 06:36:12
Advertisements