How to round the summary output in R?


To round the output of summary function in R, we can use digits argument while applying the summary function.

For example, if we have a data frame called df then to find the summary statistics with two digits in the output we can use the below given command −

summary(df,digits=2)

Example 1

Following snippet creates a dataframe −

head(iris,20)

The following dataframe is created −

 Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1    5.1       3.5         1.4         0.2         setosa
2    4.9       3.0         1.4         0.2         setosa
3    4.7       3.2         1.3         0.2         setosa
4    4.6       3.1         1.5         0.2         setosa
5    5.0       3.6         1.4         0.2         setosa
6    5.4       3.9         1.7         0.4         setosa
7    4.6       3.4         1.4         0.3         setosa
8    5.0       3.4         1.5         0.2         setosa
9    4.4       2.9         1.4         0.2         setosa
10   4.9       3.1         1.5         0.1         setosa
11   5.4       3.7         1.5         0.2         setosa
12   4.8       3.4         1.6         0.2         setosa
13   4.8       3.0         1.4         0.1         setosa
14   4.3       3.0         1.1         0.1         setosa
15   5.8       4.0         1.2         0.2         setosa
16   5.7       4.4         1.5         0.4         setosa
17   5.4       3.9         1.3         0.4         setosa
18   5.1       3.5         1.4         0.3         setosa
19   5.7       3.8         1.7         0.3         setosa
20   5.1       3.8         1.5         0.3         setosa

To round the output of summary function in R, add the following code to the above snippet −

summary(iris,digits=2)

Output

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

Sepal.Length  Sepal.Width Petal.Length Petal.Width    Species
Min. :4.3    Min. :2.0     Min. :1.0    Min. :0.1     setosa :50
1st Qu.:5.1  1st Qu.:2.8   1st Qu.:1.6  1st Qu.:0.3   versicolor:50
Median :5.8  Median :3.0   Median :4.3   Median :1.3  virginica :50
Mean :5.8    Mean :3.1     Mean :3.8     Mean :1.2
3rd Qu.:6.4  3rd Qu.:3.3   3rd Qu.:5.1   3rd Qu.:1.8
Max. :7.9    Max. :4.4     Max. :6.9     Max. :2.5

Example 2

Following snippet creates a dataframe −

head(mtcars,20)

The following dataframe is created −

                      mpg cyl disp  hp drat   wt   qsec vs am gear carb
Mazda RX4             21.0 6 160.0 110 3.90 2.620 16.46 0  1   4   4
Mazda RX4 Wag         21.0 6 160.0 110 3.90 2.875 17.02 0  1   4   4
Datsun 710            22.8 4 108.0  93 3.85 2.320 18.61 1  1   4   1
Hornet 4 Drive        21.4 6 258.0 110 3.08 3.215 19.44 1  0   3   1
Hornet Sportabout     18.7 8 360.0 175 3.15 3.440 17.02 0  0   3   2
Valiant               18.1 6 225.0 105 2.76 3.460 20.22 1  0   3   1
Duster 360            14.3 8 360.0 245 3.21 3.570 15.84 0  0   3   4
Merc 240D             24.4 4 146.7  62 3.69 3.190 20.00 1  0   4   2
Merc 230              22.8 4 140.8  95 3.92 3.150 22.90 1  0   4   2
Merc 280              19.2 6 167.6 123 3.92 3.440 18.30 1  0   4   4
Merc 280C             17.8 6 167.6 123 3.92 3.440 18.90 1  0   4   4
Merc 450SE            16.4 8 275.8 180 3.07 4.070 17.40 0  0   3   3
Merc 450SL            17.3 8 275.8 180 3.07 3.730 17.60 0  0   3   3
Merc 450SLC           15.2 8 275.8 180 3.07 3.780 18.00 0  0   3   3
Cadillac Fleetwood    10.4 8 472.0 205 2.93 5.250 17.98 0  0   3   4
Lincoln Continental   10.4 8 460.0 215 3.00 5.424 17.82 0  0   3   4
Chrysler Imperial     14.7 8 440.0 230 3.23 5.345 17.42 0  0   3   4
Fiat 128              32.4 4 78.7   66 4.08 2.200 19.47 1  1   4   1
Honda Civic           30.4 4 75.7   52 4.93 1.615 18.52 1  1   4   2
Toyota Corolla        33.9 4 71.1   65 4.22 1.835 19.90 1  1   4   1

To round the output of summary function in R, add the following code to the above snippet −

summary(mtcars,digits=5)

Output

If you execute all the above given snippets as a single program, it generates the following 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

Following snippet creates a dataframe −

head(CO2,20)

The following dataframe is created −

Grouped Data: uptake ~ conc | Plant
   Plant Type    Treatment conc uptake
1  Qn1  Quebec nonchilled   95  16.0
2  Qn1  Quebec nonchilled  175  30.4
3  Qn1  Quebec nonchilled  250  34.8
4  Qn1  Quebec nonchilled  350  37.2
5  Qn1  Quebec nonchilled  500  35.3
6  Qn1  Quebec nonchilled  675  39.2
7  Qn1  Quebec nonchilled 1000  39.7
8  Qn2  Quebec nonchilled   95  13.6
9  Qn2  Quebec nonchilled  175  27.3
10 Qn2  Quebec nonchilled  250  37.1
11 Qn2  Quebec nonchilled  350  41.8
12 Qn2  Quebec nonchilled  500  40.6
13 Qn2  Quebec nonchilled  675  41.4
14 Qn2  Quebec nonchilled 1000  44.3
15 Qn3  Quebec nonchilled   95  16.2
16 Qn3  Quebec nonchilled  175  32.4
17 Qn3  Quebec nonchilled  250  40.3
18 Qn3  Quebec nonchilled  350  42.1
19 Qn3  Quebec nonchilled  500  42.9
20 Qn3  Quebec nonchilled  675  43.9

To round the output of summary function in R, add the following code to the above snippet −

summary(CO2,digits=3)

Output

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

Plant Type Treatment conc uptake
Qn1 : 7 Quebec :42 nonchilled:42 Min. : 95 Min. : 7.7
Qn2 : 7 Mississippi:42 chilled :42 1st Qu.: 175 1st Qu.:17.9
Qn3 : 7 Median : 350 Median :28.3
Qc1 : 7 Mean : 435 Mean :27.2
Qc3 : 7 3rd Qu.: 675 3rd Qu.:37.1
Qc2 : 7 Max. :1000 Max. :45.5
(Other):42

Updated on: 23-Nov-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements