Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add percentage row to an R table

I have a data frame that contains three categorical variables that I tabulated using the following code:

df = data_frame(merged$arrears, merged$mortgage_type, merged$occupancy)
apply(df, 2, table)

And it gave me the following list:

## $`merged$arrears`
## 
##     0     1     2     3     4 
## 52124  5523  2371  1538    27 
## 
## $`merged$mortgage_type`
## 
## bullet    erm linear 
##  19958  17326  18181 
## 
## $`merged$occupancy`
## 
##            btl   holiday home owner occupied 
##          12296          27996          13112

How can I add the percentage of each category to the count in the tables of the list?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Does the tabyl() function from the janitor package solve your problem?

library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
apply(mtcars, 2, table)
#> $mpg
#> 
#> 10.4 13.3 14.3 14.7   15 15.2 15.5 15.8 16.4 17.3 17.8 18.1 18.7 19.2 19.7   21 
#>    2    1    1    1    1    2    1    1    1    1    1    1    1    2    1    2 
#> 21.4 21.5 22.8 24.4   26 27.3 30.4 32.4 33.9 
#>    2    1    2    1    1    1    2    1    1 
#> 
#> $cyl
#> 
#>  4  6  8 
#> 11  7 14 
#> 
#> $disp
#> 
#>  71.1  75.7  78.7    79  95.1   108 120.1 120.3   121 140.8   145 146.7   160 
#>     1     1     1     1     1     1     1     1     1     1     1     1     2 
#> 167.6   225   258 275.8   301   304   318   350   351   360   400   440   460 
#>     2     1     1     3     1     1     1     1     1     2     1     1     1 
#>   472 
#>     1 
#> 
#> $hp
#> 
#>  52  62  65  66  91  93  95  97 105 109 110 113 123 150 175 180 205 215 230 245 
#>   1   1   1   2   1   1   1   1   1   1   3   1   2   2   3   3   1   1   1   2 
#> 264 335 
#>   1   1 
#> 
#> $drat
#> 
#> 2.76 2.93    3 3.07 3.08 3.15 3.21 3.23 3.54 3.62 3.69  3.7 3.73 3.77 3.85  3.9 
#>    2    1    1    3    2    2    1    1    1    1    1    1    1    1    1    2 
#> 3.92 4.08 4.11 4.22 4.43 4.93 
#>    3    2    1    2    1    1 
#> 
#> $wt
#> 
#> 1.513 1.615 1.835 1.935  2.14   2.2  2.32 2.465  2.62  2.77  2.78 2.875  3.15 
#>     1     1     1     1     1     1     1     1     1     1     1     1     1 
#>  3.17  3.19 3.215 3.435  3.44  3.46  3.52  3.57  3.73  3.78  3.84 3.845  4.07 
#>     1     1     1     1     3     1     1     2     1     1     1     1     1 
#>  5.25 5.345 5.424 
#>     1     1     1 
#> 
#> $qsec
#> 
#>  14.5  14.6 15.41  15.5 15.84 16.46  16.7 16.87  16.9 17.02 17.05  17.3  17.4 
#>     1     1     1     1     1     1     1     1     1     2     1     1     1 
#> 17.42  17.6 17.82 17.98    18  18.3 18.52  18.6 18.61  18.9 19.44 19.47  19.9 
#>     1     1     1     1     1     1     1     1     1     2     1     1     1 
#>    20 20.01 20.22  22.9 
#>     1     1     1     1 
#> 
#> $vs
#> 
#>  0  1 
#> 18 14 
#> 
#> $am
#> 
#>  0  1 
#> 19 13 
#> 
#> $gear
#> 
#>  3  4  5 
#> 15 12  5 
#> 
#> $carb
#> 
#>  1  2  3  4  6  8 
#>  7 10  3 10  1  1

apply(mtcars, 2, tabyl)
#> $mpg
#>  newX[, i] n percent
#>       10.4 2 0.06250
#>       13.3 1 0.03125
#>       14.3 1 0.03125
#>       14.7 1 0.03125
#>       15.0 1 0.03125
#>       15.2 2 0.06250
#>       15.5 1 0.03125
#>       15.8 1 0.03125
#>       16.4 1 0.03125
#>       17.3 1 0.03125
#>       17.8 1 0.03125
#>       18.1 1 0.03125
#>       18.7 1 0.03125
#>       19.2 2 0.06250
#>       19.7 1 0.03125
#>       21.0 2 0.06250
#>       21.4 2 0.06250
#>       21.5 1 0.03125
#>       22.8 2 0.06250
#>       24.4 1 0.03125
#>       26.0 1 0.03125
#>       27.3 1 0.03125
#>       30.4 2 0.06250
#>       32.4 1 0.03125
#>       33.9 1 0.03125
#> 
#> $cyl
#>  newX[, i]  n percent
#>          4 11 0.34375
#>          6  7 0.21875
#>          8 14 0.43750
#> 
#> $disp
#>  newX[, i] n percent
#>       71.1 1 0.03125
#>       75.7 1 0.03125
#>       78.7 1 0.03125
#>       79.0 1 0.03125
#>       95.1 1 0.03125
#>      108.0 1 0.03125
#>      120.1 1 0.03125
#>      120.3 1 0.03125
#>      121.0 1 0.03125
#>      140.8 1 0.03125
#>      145.0 1 0.03125
#>      146.7 1 0.03125
#>      160.0 2 0.06250
#>      167.6 2 0.06250
#>      225.0 1 0.03125
#>      258.0 1 0.03125
#>      275.8 3 0.09375
#>      301.0 1 0.03125
#>      304.0 1 0.03125
#>      318.0 1 0.03125
#>      350.0 1 0.03125
#>      351.0 1 0.03125
#>      360.0 2 0.06250
#>      400.0 1 0.03125
#>      440.0 1 0.03125
#>      460.0 1 0.03125
#>      472.0 1 0.03125
#> 
#> $hp
#>  newX[, i] n percent
#>         52 1 0.03125
#>         62 1 0.03125
#>         65 1 0.03125
#>         66 2 0.06250
#>         91 1 0.03125
#>         93 1 0.03125
#>         95 1 0.03125
#>         97 1 0.03125
#>        105 1 0.03125
#>        109 1 0.03125
#>        110 3 0.09375
#>        113 1 0.03125
#>        123 2 0.06250
#>        150 2 0.06250
#>        175 3 0.09375
#>        180 3 0.09375
#>        205 1 0.03125
#>        215 1 0.03125
#>        230 1 0.03125
#>        245 2 0.06250
#>        264 1 0.03125
#>        335 1 0.03125
#> 
#> $drat
#>  newX[, i] n percent
#>       2.76 2 0.06250
#>       2.93 1 0.03125
#>       3.00 1 0.03125
#>       3.07 3 0.09375
#>       3.08 2 0.06250
#>       3.15 2 0.06250
#>       3.21 1 0.03125
#>       3.23 1 0.03125
#>       3.54 1 0.03125
#>       3.62 1 0.03125
#>       3.69 1 0.03125
#>       3.70 1 0.03125
#>       3.73 1 0.03125
#>       3.77 1 0.03125
#>       3.85 1 0.03125
#>       3.90 2 0.06250
#>       3.92 3 0.09375
#>       4.08 2 0.06250
#>       4.11 1 0.03125
#>       4.22 2 0.06250
#>       4.43 1 0.03125
#>       4.93 1 0.03125
#> 
#> $wt
#>  newX[, i] n percent
#>      1.513 1 0.03125
#>      1.615 1 0.03125
#>      1.835 1 0.03125
#>      1.935 1 0.03125
#>      2.140 1 0.03125
#>      2.200 1 0.03125
#>      2.320 1 0.03125
#>      2.465 1 0.03125
#>      2.620 1 0.03125
#>      2.770 1 0.03125
#>      2.780 1 0.03125
#>      2.875 1 0.03125
#>      3.150 1 0.03125
#>      3.170 1 0.03125
#>      3.190 1 0.03125
#>      3.215 1 0.03125
#>      3.435 1 0.03125
#>      3.440 3 0.09375
#>      3.460 1 0.03125
#>      3.520 1 0.03125
#>      3.570 2 0.06250
#>      3.730 1 0.03125
#>      3.780 1 0.03125
#>      3.840 1 0.03125
#>      3.845 1 0.03125
#>      4.070 1 0.03125
#>      5.250 1 0.03125
#>      5.345 1 0.03125
#>      5.424 1 0.03125
#> 
#> $qsec
#>  newX[, i] n percent
#>      14.50 1 0.03125
#>      14.60 1 0.03125
#>      15.41 1 0.03125
#>      15.50 1 0.03125
#>      15.84 1 0.03125
#>      16.46 1 0.03125
#>      16.70 1 0.03125
#>      16.87 1 0.03125
#>      16.90 1 0.03125
#>      17.02 2 0.06250
#>      17.05 1 0.03125
#>      17.30 1 0.03125
#>      17.40 1 0.03125
#>      17.42 1 0.03125
#>      17.60 1 0.03125
#>      17.82 1 0.03125
#>      17.98 1 0.03125
#>      18.00 1 0.03125
#>      18.30 1 0.03125
#>      18.52 1 0.03125
#>      18.60 1 0.03125
#>      18.61 1 0.03125
#>      18.90 2 0.06250
#>      19.44 1 0.03125
#>      19.47 1 0.03125
#>      19.90 1 0.03125
#>      20.00 1 0.03125
#>      20.01 1 0.03125
#>      20.22 1 0.03125
#>      22.90 1 0.03125
#> 
#> $vs
#>  newX[, i]  n percent
#>          0 18  0.5625
#>          1 14  0.4375
#> 
#> $am
#>  newX[, i]  n percent
#>          0 19 0.59375
#>          1 13 0.40625
#> 
#> $gear
#>  newX[, i]  n percent
#>          3 15 0.46875
#>          4 12 0.37500
#>          5  5 0.15625
#> 
#> $carb
#>  newX[, i]  n percent
#>          1  7 0.21875
#>          2 10 0.31250
#>          3  3 0.09375
#>          4 10 0.31250
#>          6  1 0.03125
#>          8  1 0.03125

Created on 2022-06-19 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading