Using loop to tabulate all the columns in a dataset in R
4 min readJan 29, 2023
Last time we saw how to use loops in R to automate repetitive tasks. In this quick post, we will see how to tabulate all the columns in a loop:
We will use the mtcars data:
data(mtcars)
Now the looping through all columns in the dataset:
for(i in 1:ncol(mtcars)){
# Create a frequency table for the current column
table_i <- table(mtcars[,i])
# Create a percentage distribution for the current column
prop_i <- prop.table(table_i)
# Print the frequency table and percentage distribution for the current column
print(colnames(mtcars)[i])
print(table_i)
print(prop_i)
}
And a happy result:
[1] "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
2 1 1 1 1 2 1 1 1 1 1 1 1 2 1
21 21.4 21.5 22.8 24.4 26 27.3 30.4 32.4 33.9
2 2 1 2 1 1 1 2 1 1
10.4 13.3 14.3 14.7 15 15.2 15.5 15.8 16.4
0.06250 0.03125 0.03125 0.03125 0.03125 0.06250 0.03125 0.03125 0.03125
17.3 17.8 18.1 18.7 19.2 19.7 21 21.4 21.5
0.03125 0.03125 0.03125 0.03125 0.06250 0.03125 0.06250 0.06250 0.03125
22.8 24.4 26 27.3 30.4 32.4 33.9
0.06250 0.03125 0.03125 0.03125 0.06250 0.03125 0.03125
[1] "cyl"
4 6 8
11 7 14
4 6 8
0.34375 0.21875 0.43750
[1] "disp"
71.1 75.7 78.7 79 95.1 108 120.1 120.3 121 140.8 145 146.7
1 1 1 1 1 1 1 1 1 1 1 1
160 167.6 225 258 275.8 301 304 318 350 351 360 400
2 2 1 1 3 1 1 1 1 1 2 1
440 460 472
1 1 1
71.1 75.7 78.7 79 95.1 108 120.1 120.3 121
0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
140.8 145 146.7 160 167.6 225 258 275.8 301
0.03125 0.03125 0.03125 0.06250 0.06250 0.03125 0.03125 0.09375 0.03125
304 318 350 351 360 400 440 460 472
0.03125 0.03125 0.03125 0.03125 0.06250 0.03125 0.03125 0.03125 0.03125
[1] "hp"
52 62 65 66 91 93 95 97 105 109 110 113 123 150 175 180 205 215 230
1 1 1 2 1 1 1 1 1 1 3 1 2 2 3 3 1 1 1
245 264 335
2 1 1
52 62 65 66 91 93 95 97 105
0.03125 0.03125 0.03125 0.06250 0.03125 0.03125 0.03125 0.03125 0.03125
109 110 113 123 150 175 180 205 215
0.03125 0.09375 0.03125 0.06250 0.06250 0.09375 0.09375 0.03125 0.03125
230 245 264 335
0.03125 0.06250 0.03125 0.03125
[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
2 1 1 3 2 2 1 1 1 1 1 1 1 1 1
3.9 3.92 4.08 4.11 4.22 4.43 4.93
2 3 2 1 2 1 1
2.76 2.93 3 3.07 3.08 3.15 3.21 3.23 3.54
0.06250 0.03125 0.03125 0.09375 0.06250 0.06250 0.03125 0.03125 0.03125
3.62 3.69 3.7 3.73 3.77 3.85 3.9 3.92 4.08
0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.06250 0.09375 0.06250
4.11 4.22 4.43 4.93
0.03125 0.06250 0.03125 0.03125
[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
1 1 1 1 1 1 1 1 1 1 1 1
3.15 3.17 3.19 3.215 3.435 3.44 3.46 3.52 3.57 3.73 3.78 3.84
1 1 1 1 1 3 1 1 2 1 1 1
3.845 4.07 5.25 5.345 5.424
1 1 1 1 1
1.513 1.615 1.835 1.935 2.14 2.2 2.32 2.465 2.62
0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
2.77 2.78 2.875 3.15 3.17 3.19 3.215 3.435 3.44
0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.09375
3.46 3.52 3.57 3.73 3.78 3.84 3.845 4.07 5.25
0.03125 0.03125 0.06250 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
5.345 5.424
0.03125 0.03125
[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
1 1 1 1 1 1 1 1 1 2 1 1
17.4 17.42 17.6 17.82 17.98 18 18.3 18.52 18.6 18.61 18.9 19.44
1 1 1 1 1 1 1 1 1 1 2 1
19.47 19.9 20 20.01 20.22 22.9
1 1 1 1 1 1
14.5 14.6 15.41 15.5 15.84 16.46 16.7 16.87 16.9
0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
17.02 17.05 17.3 17.4 17.42 17.6 17.82 17.98 18
0.06250 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125 0.03125
18.3 18.52 18.6 18.61 18.9 19.44 19.47 19.9 20
0.03125 0.03125 0.03125 0.03125 0.06250 0.03125 0.03125 0.03125 0.03125
20.01 20.22 22.9
0.03125 0.03125 0.03125
[1] "vs"
0 1
18 14
0 1
0.5625 0.4375
[1] "am"
0 1
19 13
0 1
0.59375 0.40625
[1] "gear"
3 4 5
15 12 5
3 4 5
0.46875 0.37500 0.15625
[1] "carb"
1 2 3 4 6 8
7 10 3 10 1 1
1 2 3 4 6 8
0.21875 0.31250 0.09375 0.31250 0.03125 0.03125
>