[R] Format printing with R

Andrew Simmons @kw@|mmo @end|ng |rom gm@||@com
Mon Nov 21 22:41:37 CET 2022


For print(), digits is the minimal number of significant digits. In
your case, rounding the first column to the 3rd decimal place gives at
least 2 sigfigs and rounding the second column to the 2nd decimal
place.

If you want to print all numbers to two significant digits, regardless
of what other numbers are in the column, use signif() before print():

Mean <- c(0.3107966, 0.1880302, 45.3185794, 0.506637, 0.493363,
    0.702915, 0.297085, 0.7967066, 0.2032934, 0.6582301, 0.3417699,
    0.7262913, 0.2737087, 0.6415484, 0.3584516, 0.9110264, 0.0889736,
    0.5211453, 0.4788547, 0.5481055, 0.4518945, 0.913509, 0.086491,
    0.8727269, 0.1272731, 0.1015717, 0.6043692, 0.2940592, 0.2735274,
    0.3777426, 0.34873, 0.1603127, 0.1723783, 0.1230961, 0.1779381,
    0.0964334, 0.1584698, 0.1113717, 0.3349813, 0.4081109, 0.2569078,
    0.1034356, 0.6741233, 0.1254412, 0.0969999, 0.0587457, 0.4401115,
    0.4689114, 0.0322313, 0.5907618, 0.1591195, 0.1132923, 0.1124207,
    0.0244058, 0.7058787, 0.2941213, 0.0746892, 0.474911, 0.3471837,
    0.0435036, 0.0597126, 0.0478775, 0.1152615, 0.2074968, 0.2440626,
    0.1605995, 0.0804598, 0.1442422, 0.3443231, 0.428056, 0.0528221,
    0.0805222, 0.0457169, 0.0485596, 0.1333443, 0.0932917, 0.0653987,
    0.0573934, 0.1399086, 0.0887337, 0.0984479, 0.0914421, 0.1155505,
    0.1363764, 0.113457, 1.2985286)
Std.dev <- c(0.46282, 0.390736, 16.313635, 0.499956, 0.499956,
    0.456974, 0.456974, 0.402449, 0.402449, 0.474303, 0.474303, 0.445861,
    0.445861, 0.479546, 0.479546, 0.284706, 0.284706, 0.499553, 0.499553,
    0.49768, 0.49768, 0.281088, 0.281088, 0.333279, 0.333279, 0.302084,
    0.488986, 0.455619, 0.445769, 0.484823, 0.476568, 0.366896, 0.377709,
    0.328547, 0.382461, 0.295185, 0.365181, 0.314592, 0.471984, 0.491484,
    0.436928, 0.304527, 0.468701, 0.331218, 0.295958, 0.235148, 0.4964,
    0.499033, 0.176614, 0.491693, 0.365787, 0.31695, 0.315883, 0.154305,
    0.455647, 0.455647, 0.262889, 0.49937, 0.476075, 0.203988, 0.236954,
    0.213507, 0.319337, 0.405514, 0.42953, 0.367161, 0.272004, 0.351335,
    0.475147, 0.494797, 0.223678, 0.2721, 0.20887, 0.214945, 0.339946,
    0.290841, 0.247228, 0.232593, 0.346892, 0.284359, 0.297919, 0.288237,
    0.319685, 0.343188, 0.317151, 0.739096)
print(signif(cbind(Mean, Std.dev), 2))

which looks like:

> print(signif(cbind(Mean, Std.dev), 2))
        Mean Std.dev
 [1,]  0.310    0.46
 [2,]  0.190    0.39
 [3,] 45.000   16.00
 [4,]  0.510    0.50
 [5,]  0.490    0.50
 [6,]  0.700    0.46
 [7,]  0.300    0.46
 [8,]  0.800    0.40
 [9,]  0.200    0.40
[10,]  0.660    0.47
[11,]  0.340    0.47
[12,]  0.730    0.45
[13,]  0.270    0.45
[14,]  0.640    0.48
[15,]  0.360    0.48
[16,]  0.910    0.28
[17,]  0.089    0.28
[18,]  0.520    0.50
[19,]  0.480    0.50
[20,]  0.550    0.50
[21,]  0.450    0.50
[22,]  0.910    0.28
[23,]  0.086    0.28
[24,]  0.870    0.33
[25,]  0.130    0.33
[26,]  0.100    0.30
[27,]  0.600    0.49
[28,]  0.290    0.46
[29,]  0.270    0.45
[30,]  0.380    0.48
[31,]  0.350    0.48
[32,]  0.160    0.37
[33,]  0.170    0.38
[34,]  0.120    0.33
[35,]  0.180    0.38
[36,]  0.096    0.30
[37,]  0.160    0.37
[38,]  0.110    0.31
[39,]  0.330    0.47
[40,]  0.410    0.49
[41,]  0.260    0.44
[42,]  0.100    0.30
[43,]  0.670    0.47
[44,]  0.130    0.33
[45,]  0.097    0.30
[46,]  0.059    0.24
[47,]  0.440    0.50
[48,]  0.470    0.50
[49,]  0.032    0.18
[50,]  0.590    0.49
[51,]  0.160    0.37
[52,]  0.110    0.32
[53,]  0.110    0.32
[54,]  0.024    0.15
[55,]  0.710    0.46
[56,]  0.290    0.46
[57,]  0.075    0.26
[58,]  0.470    0.50
[59,]  0.350    0.48
[60,]  0.044    0.20
[61,]  0.060    0.24
[62,]  0.048    0.21
[63,]  0.120    0.32
[64,]  0.210    0.41
[65,]  0.240    0.43
[66,]  0.160    0.37
[67,]  0.080    0.27
[68,]  0.140    0.35
[69,]  0.340    0.48
[70,]  0.430    0.49
[71,]  0.053    0.22
[72,]  0.081    0.27
[73,]  0.046    0.21
[74,]  0.049    0.21
[75,]  0.130    0.34
[76,]  0.093    0.29
[77,]  0.065    0.25
[78,]  0.057    0.23
[79,]  0.140    0.35
[80,]  0.089    0.28
[81,]  0.098    0.30
[82,]  0.091    0.29
[83,]  0.120    0.32
[84,]  0.140    0.34
[85,]  0.110    0.32
[86,]  1.300    0.74
>

R will still print 3 decimal places for the third column since it
wants them to be of the same format, but each number is 2 sigfigs.

On Mon, Nov 21, 2022 at 3:41 PM Steven T. Yen via R-help
<r-help using r-project.org> wrote:
>
> Hi, I have two variables with 86 observations each. Below I print with
> the print command with digit=2. But, I am getting three decimal places
> for my first variable and two for the second. Please help. Thanks.
>
>  > cbind(Mean,Std.dev)
>              Mean   Std.dev
>   [1,]  0.3107966  0.462820
>   [2,]  0.1880302  0.390736
>   [3,] 45.3185794 16.313635
>   [4,]  0.5066370  0.499956
>   [5,]  0.4933630  0.499956
>   [6,]  0.7029150  0.456974
>   [7,]  0.2970850  0.456974
>   [8,]  0.7967066  0.402449
>   [9,]  0.2032934  0.402449
> [10,]  0.6582301  0.474303
> [11,]  0.3417699  0.474303
> [12,]  0.7262913  0.445861
> [13,]  0.2737087  0.445861
> [14,]  0.6415484  0.479546
> [15,]  0.3584516  0.479546
> [16,]  0.9110264  0.284706
> [17,]  0.0889736  0.284706
> [18,]  0.5211453  0.499553
> [19,]  0.4788547  0.499553
> [20,]  0.5481055  0.497680
> [21,]  0.4518945  0.497680
> [22,]  0.9135090  0.281088
> [23,]  0.0864910  0.281088
> [24,]  0.8727269  0.333279
> [25,]  0.1272731  0.333279
> [26,]  0.1015717  0.302084
> [27,]  0.6043692  0.488986
> [28,]  0.2940592  0.455619
> [29,]  0.2735274  0.445769
> [30,]  0.3777426  0.484823
> [31,]  0.3487300  0.476568
> [32,]  0.1603127  0.366896
> [33,]  0.1723783  0.377709
> [34,]  0.1230961  0.328547
> [35,]  0.1779381  0.382461
> [36,]  0.0964334  0.295185
> [37,]  0.1584698  0.365181
> [38,]  0.1113717  0.314592
> [39,]  0.3349813  0.471984
> [40,]  0.4081109  0.491484
> [41,]  0.2569078  0.436928
> [42,]  0.1034356  0.304527
> [43,]  0.6741233  0.468701
> [44,]  0.1254412  0.331218
> [45,]  0.0969999  0.295958
> [46,]  0.0587457  0.235148
> [47,]  0.4401115  0.496400
> [48,]  0.4689114  0.499033
> [49,]  0.0322313  0.176614
> [50,]  0.5907618  0.491693
> [51,]  0.1591195  0.365787
> [52,]  0.1132923  0.316950
> [53,]  0.1124207  0.315883
> [54,]  0.0244058  0.154305
> [55,]  0.7058787  0.455647
> [56,]  0.2941213  0.455647
> [57,]  0.0746892  0.262889
> [58,]  0.4749110  0.499370
> [59,]  0.3471837  0.476075
> [60,]  0.0435036  0.203988
> [61,]  0.0597126  0.236954
> [62,]  0.0478775  0.213507
> [63,]  0.1152615  0.319337
> [64,]  0.2074968  0.405514
> [65,]  0.2440626  0.429530
> [66,]  0.1605995  0.367161
> [67,]  0.0804598  0.272004
> [68,]  0.1442422  0.351335
> [69,]  0.3443231  0.475147
> [70,]  0.4280560  0.494797
> [71,]  0.0528221  0.223678
> [72,]  0.0805222  0.272100
> [73,]  0.0457169  0.208870
> [74,]  0.0485596  0.214945
> [75,]  0.1333443  0.339946
> [76,]  0.0932917  0.290841
> [77,]  0.0653987  0.247228
> [78,]  0.0573934  0.232593
> [79,]  0.1399086  0.346892
> [80,]  0.0887337  0.284359
> [81,]  0.0984479  0.297919
> [82,]  0.0914421  0.288237
> [83,]  0.1155505  0.319685
> [84,]  0.1363764  0.343188
> [85,]  0.1134570  0.317151
> [86,]  1.2985286  0.739096
>  > print(cbind(Mean,Std.dev),digits=2)
>          Mean Std.dev
>   [1,]  0.311    0.46
>   [2,]  0.188    0.39
>   [3,] 45.319   16.31
>   [4,]  0.507    0.50
>   [5,]  0.493    0.50
>   [6,]  0.703    0.46
>   [7,]  0.297    0.46
>   [8,]  0.797    0.40
>   [9,]  0.203    0.40
> [10,]  0.658    0.47
> [11,]  0.342    0.47
> [12,]  0.726    0.45
> [13,]  0.274    0.45
> [14,]  0.642    0.48
> [15,]  0.358    0.48
> [16,]  0.911    0.28
> [17,]  0.089    0.28
> [18,]  0.521    0.50
> [19,]  0.479    0.50
> [20,]  0.548    0.50
> [21,]  0.452    0.50
> [22,]  0.914    0.28
> [23,]  0.086    0.28
> [24,]  0.873    0.33
> [25,]  0.127    0.33
> [26,]  0.102    0.30
> [27,]  0.604    0.49
> [28,]  0.294    0.46
> [29,]  0.274    0.45
> [30,]  0.378    0.48
> [31,]  0.349    0.48
> [32,]  0.160    0.37
> [33,]  0.172    0.38
> [34,]  0.123    0.33
> [35,]  0.178    0.38
> [36,]  0.096    0.30
> [37,]  0.158    0.37
> [38,]  0.111    0.31
> [39,]  0.335    0.47
> [40,]  0.408    0.49
> [41,]  0.257    0.44
> [42,]  0.103    0.30
> [43,]  0.674    0.47
> [44,]  0.125    0.33
> [45,]  0.097    0.30
> [46,]  0.059    0.24
> [47,]  0.440    0.50
> [48,]  0.469    0.50
> [49,]  0.032    0.18
> [50,]  0.591    0.49
> [51,]  0.159    0.37
> [52,]  0.113    0.32
> [53,]  0.112    0.32
> [54,]  0.024    0.15
> [55,]  0.706    0.46
> [56,]  0.294    0.46
> [57,]  0.075    0.26
> [58,]  0.475    0.50
> [59,]  0.347    0.48
> [60,]  0.044    0.20
> [61,]  0.060    0.24
> [62,]  0.048    0.21
> [63,]  0.115    0.32
> [64,]  0.207    0.41
> [65,]  0.244    0.43
> [66,]  0.161    0.37
> [67,]  0.080    0.27
> [68,]  0.144    0.35
> [69,]  0.344    0.48
> [70,]  0.428    0.49
> [71,]  0.053    0.22
> [72,]  0.081    0.27
> [73,]  0.046    0.21
> [74,]  0.049    0.21
> [75,]  0.133    0.34
> [76,]  0.093    0.29
> [77,]  0.065    0.25
> [78,]  0.057    0.23
> [79,]  0.140    0.35
> [80,]  0.089    0.28
> [81,]  0.098    0.30
> [82,]  0.091    0.29
> [83,]  0.116    0.32
> [84,]  0.136    0.34
> [85,]  0.113    0.32
> [86,]  1.299    0.74
>  >
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list