How to format all decimal places in an R vector and data frame?

R ProgrammingServer Side ProgrammingProgramming

To format all decimal places in an R vector and data frame, we can use formattable function of formattable package where we can specify the number of digits after decimal places. For example, if we have a numerical vector say x then the values in x can be formatted to have only 2 decimal places by using the command formattable(x,format="f",digits=2).

Example1

Loading formattable package −

library(formattable)

 Live Demo

x1<-sample(0:9,200,replace=TRUE)
x1

Output

[1]   5 1 5 5 5 3 3 6 8 6 6 5 5 1 3 9 4 2 1 3 4 5 7 4 0 2 6 3 4 6 9 0 1 4 0 4 8
[38]  4 2 8 9 8 0 6 2 9 1 5 0 9 1 3 7 1 7 2 6 8 8 0 8 4 4 7 8 5 3 0 3 6 4 1 5 7
[75]  0 7 9 5 3 4 4 7 3 2 2 1 4 5 2 4 3 8 7 2 5 3 3 7 8 7 2 0 3 1 6 7 9 8 9 4 2
[112] 8 4 6 9 3 1 0 9 4 4 4 7 9 7 7 6 6 4 1 3 1 8 9 1 6 6 5 7 8 3 9 3 5 4 1 4 0
[149] 7 9 6 4 3 4 1 4 0 4 0 0 9 1 0 4 7 1 2 3 0 0 3 9 5 0 5 9 2 6 8 1 1 1 7 3 1
[186] 6 2 6 8 5 1 4 9 8 8 4 4 1 0 0
x1<-formattable(x1,format="f",digits=2)
x1
[1]   5.00 1.00 5.00 5.00 5.00 3.00 3.00 6.00 8.00 6.00 6.00 5.00 5.00 1.00 3.00
[16]  9.00 4.00 2.00 1.00 3.00 4.00 5.00 7.00 4.00 0.00 2.00 6.00 3.00 4.00 6.00
[31]  9.00 0.00 1.00 4.00 0.00 4.00 8.00 4.00 2.00 8.00 9.00 8.00 0.00 6.00 2.00
[46]  9.00 1.00 5.00 0.00 9.00 1.00 3.00 7.00 1.00 7.00 2.00 6.00 8.00 8.00 0.00
[61]  8.00 4.00 4.00 7.00 8.00 5.00 3.00 0.00 3.00 6.00 4.00 1.00 5.00 7.00 0.00
[76]  7.00 9.00 5.00 3.00 4.00 4.00 7.00 3.00 2.00 2.00 1.00 4.00 5.00 2.00 4.00
[91]  3.00 8.00 7.00 2.00 5.00 3.00 3.00 7.00 8.00 7.00 2.00 0.00 3.00 1.00 6.00
[106] 7.00 9.00 8.00 9.00 4.00 2.00 8.00 4.00 6.00 9.00 3.00 1.00 0.00 9.00 4.00
[121] 4.00 4.00 7.00 9.00 7.00 7.00 6.00 6.00 4.00 1.00 3.00 1.00 8.00 9.00 1.00
[136] 6.00 6.00 5.00 7.00 8.00 3.00 9.00 3.00 5.00 4.00 1.00 4.00 0.00 7.00 9.00
[151] 6.00 4.00 3.00 4.00 1.00 4.00 0.00 4.00 0.00 0.00 9.00 1.00 0.00 4.00 7.00
[166] 1.00 2.00 3.00 0.00 0.00 3.00 9.00 5.00 0.00 5.00 9.00 2.00 6.00 8.00 1.00
[181] 1.00 1.00 7.00 3.00 1.00 6.00 2.00 6.00 8.00 5.00 1.00 4.00 9.00 8.00 8.00
[196] 4.00 4.00 1.00 0.00 0.00

Example2

 Live Demo

x2<-rnorm(80)
x2

Output

[1]  -1.29604051 0.46330591 0.78263447 -1.34445199 -0.79244441 0.91579428
[7]  -0.20265807 0.19579780 1.54057571 -1.15957283 -0.61925230 -1.14279112
[13]  0.32428064 0.18469994 1.00994660 -0.38836171 0.62360893 -0.84954427
[19]  0.33794565 -0.68543650 1.03641121 -1.39719529 1.19779327 -0.28556066
[25] -1.90227084 -0.14752750 1.59804623 0.11688348 -0.22111880 -0.80496219
[31]  1.94998003 -0.44815429 -1.45236881 0.46414144 1.02714470 -0.35517641
[37] -1.91672346 0.33816514 0.02611275 2.52803119 0.42948063 1.41334339
[43] -0.27168314 -0.26893631 -0.79865763 0.69654229 1.30060073 -2.66033456
[49] -1.05879712 -0.70326509 0.44590006 0.22191097 1.52717953 0.88452961
[55] -0.79603885 1.44314800 0.69391092 -0.27902754 -0.85581926 -2.32344161
[61]  0.19283909 0.80069953 0.66911613 -1.04232630 2.14785749 1.74218280
[67]  2.09058219 -0.03829720 -0.48808007 -2.13874852 0.08684361 0.47503292
[73] -0.27623372 -0.78762974 1.19659235 -0.43385002 -1.00537520 0.23216263
[79]  2.36660415 1.07115586
x2<-formattable(x2,format="f",digits=2)
x2
[1]  -1.30 0.46 0.78 -1.34 -0.79 0.92 -0.20 0.20 1.54 -1.16 -0.62 -1.14
[13]  0.32 0.18 1.01 -0.39 0.62 -0.85 0.34 -0.69 1.04 -1.40 1.20 -0.29
[25] -1.90 -0.15 1.60 0.12 -0.22 -0.80 1.95 -0.45 -1.45 0.46 1.03 -0.36
[37] -1.92 0.34 0.03 2.53 0.43 1.41 -0.27 -0.27 -0.80 0.70 1.30 -2.66
[49] -1.06 -0.70 0.45 0.22 1.53 0.88 -0.80 1.44 0.69 -0.28 -0.86 -2.32
[61]  0.19 0.80 0.67 -1.04 2.15 1.74 2.09 -0.04 -0.49 -2.14 0.09 0.48
[73] -0.28 -0.79 1.20 -0.43 -1.01 0.23 2.37 1.07

Example3

 Live Demo

x3<-runif(80,2,10)
x3

Output

[1]  7.081306 9.350415 4.351160 4.605524 2.207032 2.195813 7.951999 9.743476
[9]  6.298917 5.333969 7.378064 5.690359 9.342053 2.498212 6.846488 4.782182
[17] 7.988461 2.716306 8.722352 8.400760 3.900622 7.034838 7.943670 9.448758
[25] 7.083654 4.126258 2.582828 9.135599 7.766064 5.453632 5.884645 7.990329
[33] 3.620905 9.892212 3.845167 8.471849 5.911369 8.215719 7.617544 7.759089
[41] 6.751566 8.302347 4.073309 6.493836 8.066407 9.242996 6.263305 6.255265
[49] 4.220699 5.700430 9.602641 4.467327 7.147127 6.307310 2.621831 6.085484
[57] 9.279844 8.147505 4.756649 9.389553 8.030251 7.569268 5.930407 5.020778
[65] 3.925739 9.593744 4.686891 8.079335 8.041129 2.873302 8.414689 8.780803
[73] 4.486050 3.732873 6.186431 9.517897 5.879295 8.485993 6.257261 9.541974
x3<-formattable(x3,format="f",digits=2)
x3
[1]  7.08 9.35 4.35 4.61 2.21 2.20 7.95 9.74 6.30 5.33 7.38 5.69 9.34 2.50 6.85
[16] 4.78 7.99 2.72 8.72 8.40 3.90 7.03 7.94 9.45 7.08 4.13 2.58 9.14 7.77 5.45
[31] 5.88 7.99 3.62 9.89 3.85 8.47 5.91 8.22 7.62 7.76 6.75 8.30 4.07 6.49 8.07
[46] 9.24 6.26 6.26 4.22 5.70 9.60 4.47 7.15 6.31 2.62 6.09 9.28 8.15 4.76 9.39
[61] 8.03 7.57 5.93 5.02 3.93 9.59 4.69 8.08 8.04 2.87 8.41 8.78 4.49 3.73 6.19
[76] 9.52 5.88 8.49 6.26 9.54

Example4

 Live Demo

iv1<-rnorm(20)
iv2<-rnorm(20)
resp<-rnorm(20,78,3.5)
df1<-data.frame(iv1,iv2,resp)
df1

Output

      iv1         iv2          resp
1   1.03398540   0.07254862  75.99465
2  -0.09504870   0.13830547  72.74974
3   1.13174190   2.05943775  77.33238
4  -0.02728544  -2.19835582  74.54968
5   0.53777918  -0.60683616  82.39609
6  -0.14458976  -0.06022428  79.49349
7  -0.71368075  -0.29802388  78.03228
8  -1.62761198  -0.50938475  81.42240
9  -0.03233459   0.94152424  86.12316
10 -2.50062742  -0.05108768  75.94005
11  0.11128679  -0.05781440  72.02964
12 -0.05132032   1.62225539  77.66552
13 -0.49332274  -0.22909814  76.69032
14 -0.09782262   1.08388816  74.82925
15 -1.56938242   0.27308632  82.71712
16  0.13721036  -1.05074764  68.95937
17  0.30667417  -2.62395368  78.83784
18 -0.41926090   1.01394042  75.01351
19 -0.88518665  -0.82639519  83.28899
20  0.89221821   0.61685789  80.80044

Example5

df1$iv1<-formattable(df1$iv1,format="f",digits=2)
df1$iv2<-formattable(df1$iv2,format="f",digits=2)
df1$resp<-formattable(df1$resp,format="f",digits=2)
df1

Output

    iv1    iv2    resp
1   1.03   0.07  75.99
2  -0.10   0.14  72.75
3   1.13   2.06  77.33
4  -0.03  -2.20  74.55
5   0.54  -0.61  82.40
6  -0.14  -0.06  79.49
7  -0.71  -0.30  78.03
8  -1.63  -0.51  81.42
9  -0.03   0.94  86.12
10 -2.50  -0.05  75.94
11  0.11  -0.06  72.03
12 -0.05   1.62  77.67
13 -0.49  -0.23  76.69
14 -0.10   1.08  74.83
15 -1.57   0.27  82.72
16  0.14  -1.05  68.96
17  0.31  -2.62  78.84
18 -0.42   1.01  75.01
19 -0.89  -0.83  83.29
20  0.89   0.62  80.80
raja
Published on 06-Mar-2021 12:14:13
Advertisements