How to set the number of digits to be printed for summary command without using options(digits) in R?


To set the nuber of digits to be printed for summary command without using options(digits), we can use digits argument while printing the summary. −

Example 1

Using mtcars data and finding the summary statistics with number of digits set to 2 −

 Live Demo

summary(mtcars,digits=2)

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

Output

   mpg             cyl            disp            hp          drat
Min.   :10    Min.   :4.0    Min.   : 71    Min.   : 52    Min.   :2.8
1st Qu.:15    1st Qu.:4.0    1st Qu.:121    1st Qu.: 96    1st Qu.:3.1
Median :19    Median :6.0    Median :196    Median :123    Median :3.7
Mean   :20    Mean   :6.2    Mean   :231    Mean   :147    Mean   :3.6
3rd Qu.:23    3rd Qu.:8.0    3rd Qu.:326    3rd Qu.:180    3rd Qu.:3.9
Max.   :34    Max.   :8.0    Max.   :472    Max.   :335    Max.   :4.9

      wt          qsec             vs             am             gear
Min.   :1.5    Min.   :14    Min.   :0.00    Min.   :0.00    Min.   :3.0
1st Qu.:2.6    1st Qu.:17    1st Qu.:0.00    1st Qu.:0.00    1st Qu.:3.0
Median :3.3    Median :18    Median :0.00    Median :0.00    Median :4.0
Mean   :3.2    Mean   :18    Mean   :0.44    Mean   :0.41    Mean   :3.7
3rd Qu.:3.6    3rd Qu.:19    3rd Qu.:1.00    3rd Qu.:1.00    3rd Qu.:4.0
Max.   :5.4    Max.   :23    Max.   :1.00    Max.   :1.00    Max.   :5.0

   carb
Min.   :1.0
1st Qu.:2.0
Median :2.0
Mean   :2.8
3rd Qu.:4.0
Max.   :8.0

Example 2

Using mtcars data and finding the summary statistics with number of digits set to 5 −

 Live Demo

summary(mtcars,digits=5)

Output

   mpg                  cyl             disp                hp
Min.   :10.400    Min.   :4.0000    Min.   : 71.10    Min.   : 52.00
1st Qu.:15.425    1st Qu.:4.0000    1st Qu.:120.83    1st Qu.: 96.50
Median :19.200    Median :6.0000    Median :196.30    Median :123.00
Mean   :20.091    Mean   :6.1875    Mean   :230.72    Mean   :146.69
3rd Qu.:22.800    3rd Qu.:8.0000    3rd Qu.:326.00    3rd Qu.:180.00
Max.   :33.900    Max.   :8.0000    Max.   :472.00    Max.   :335.00

      drat             wt                qsec             vs
Min.   :2.7600    Min.   :1.5130    Min.   :14.500    Min.   :0.0000
1st Qu.:3.0800    1st Qu.:2.5812    1st Qu.:16.892    1st Qu.:0.0000
Median :3.6950    Median :3.3250    Median :17.710    Median :0.0000
Mean   :3.5966    Mean   :3.2172    Mean   :17.849    Mean   :0.4375
3rd Qu.:3.9200    3rd Qu.:3.6100    3rd Qu.:18.900    3rd Qu.:1.0000
Max.   :4.9300    Max.   :5.4240    Max.   :22.900    Max.   :1.0000
      am                gear             carb
Min.   :0.00000    Min.   :3.0000    Min.   :1.0000
1st Qu.:0.00000    1st Qu.:3.0000    1st Qu.:2.0000
Median :0.00000    Median :4.0000    Median :2.0000
Mean   :0.40625    Mean   :3.6875    Mean   :2.8125
3rd Qu.:1.00000    3rd Qu.:4.0000    3rd Qu.:4.0000
Max.   :1.00000    Max.   :5.0000    Max.   :8.0000

Example 3

Using mtcars data and finding the summary statistics with number of digits set to 6 −

 Live Demo

summary(mtcars,digits=6)

Output

      mpg                cyl             disp                hp
Min.   :10.4000    Min.   :4.0000    Min.   : 71.100    Min.   : 52.000
1st Qu.:15.4250    1st Qu.:4.0000    1st Qu.:120.825    1st Qu.: 96.500
Median :19.2000    Median :6.0000    Median :196.300    Median :123.000
Mean   :20.0906    Mean   :6.1875    Mean   :230.722    Mean   :146.688
3rd Qu.:22.8000    3rd Qu.:8.0000    3rd Qu.:326.000    3rd Qu.:180.000
Max.   :33.9000    Max.   :8.0000    Max.   :472.000    Max.   :335.000

      drat                wt                qsec             vs
Min.   :2.76000    Min.   :1.51300    Min.   :14.5000    Min.   :0.0000
1st Qu.:3.08000    1st Qu.:2.58125    1st Qu.:16.8925    1st Qu.:0.0000
Median :3.69500    Median :3.32500    Median :17.7100    Median :0.0000
Mean   :3.59656    Mean   :3.21725    Mean   :17.8487    Mean   :0.4375
3rd Qu.:3.92000    3rd Qu.:3.61000    3rd Qu.:18.9000    3rd Qu.:1.0000
Max.   :4.93000    Max.    :5.42400    Max.  :22.9000    Max.   :1.0000

      am                gear             carb
Min.   :0.00000    Min.   :3.0000    Min.   :1.0000
1st Qu.:0.00000    1st Qu.:3.0000    1st Qu.:2.0000
Median :0.00000    Median :4.0000    Median :2.0000
Mean   :0.40625    Mean   :3.6875    Mean   :2.8125
3rd Qu.:1.00000    3rd Qu.:4.0000    3rd Qu.:4.0000
Max.   :1.00000    Max.   :5.0000    Max.   :8.0000

Example 4

Using mtcars data and finding the summary statistics with number of digits set to 3 −

 Live Demo

summary(mtcars,digits=3)

Output

      mpg             cyl             disp             hp             drat
Min.   :10.4    Min.   :4.00    Min.   : 71.1    Min.   : 52.0    Min.   :2.76
1st Qu.:15.4    1st Qu.:4.00    1st Qu.:120.8    1st Qu.: 96.5    1st Qu.:3.08
Median :19.2    Median :6.00    Median :196.3    Median :123.0    Median :3.69
Mean   :20.1    Mean   :6.19    Mean   :230.7    Mean   :146.7    Mean   :3.60
3rd Qu.:22.8    3rd Qu.:8.00    3rd Qu.:326.0    3rd Qu.:180.0    3rd Qu.:3.92
Max.   :33.9    Max.   :8.00    Max.   :472.0    Max.   :335.0    Max.   :4.93

      wt             qsec             vs                am             gear
Min.   :1.51    Min.   :14.5    Min.   :0.000    Min.   :0.000    Min.   :3.00
1st Qu.:2.58    1st Qu.:16.9    1st Qu.:0.000    1st Qu.:0.000    1st Qu.:3.00
Median :3.33    Median :17.7    Median :0.000    Median :0.000    Median :4.00
Mean   :3.22    Mean   :17.8    Mean   :0.438    Mean   :0.406    Mean   :3.69
3rd Qu.:3.61    3rd Qu.:18.9    3rd Qu.:1.000    3rd Qu.:1.000    3rd Qu.:4.00
Max.   :5.42    Max.   :22.9    Max.   :1.000    Max.   :1.000    Max.   :5.00

      carb
Min.   :1.00
1st Qu.:2.00
Median :2.00
Mean   :2.81
3rd Qu.:4.00
Max.   :8.00

Updated on: 13-Aug-2021

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements