How to Round All Elements in a Matrix Object in R (Example Code)

This tutorial demonstrates how to round all elements in a matrix in the R programming language.

Creation of Example Data

set.seed(263574)            # Constructing a random matrix object
x <- matrix(rnorm(100), nrow = 10)
x
#              [,1]        [,2]        [,3]       [,4]       [,5]         [,6]
#  [1,]  0.26869128  1.17024993  0.66082857  0.5442171 -1.3444381  0.007195177
#  [2,] -0.12086650 -0.32982877 -0.72682212  2.0862790  0.3491467 -0.125403280
#  [3,] -0.84928767  0.90188159 -1.43514258 -0.2761964  1.3561400 -0.792956094
#  [4,]  0.02101204  0.33133713  0.44686659 -0.3450884 -1.0455122 -0.288181379
#  [5,]  0.98045038  0.22686031 -0.09395685 -0.9648132 -0.3948321 -0.362389082
#  [6,]  0.72795399 -0.31996693 -1.95826866  0.5933404 -0.2879974 -0.960440868
#  [7,]  0.90026362  0.28850533  0.79540265 -0.9519669  0.5645309  0.490191887
#  [8,]  1.32871063 -0.08062973 -1.78235017 -1.9501446  0.6261024  0.089698571
#  [9,] -0.27001824 -0.34358827 -0.32707203 -0.9734236  0.2407501 -0.994067365
# [10,] -1.08748983  0.73928380 -1.28716169 -0.6853608 -0.1316238  2.751864643
#              [,7]        [,8]        [,9]        [,10]
#  [1,] -0.08463995 -0.09160081 -1.39765995  0.326096052
#  [2,] -0.54732885  0.67416161 -0.07174579 -1.503745823
#  [3,]  1.82658254 -0.56307885  0.80113405  0.532236712
#  [4,]  0.65696536  0.77774257 -1.84516059  0.858419489
#  [5,]  0.05342192 -0.47569658 -0.45513351  2.300721164
#  [6,]  2.01271853 -1.45945331  0.33861015  0.962581345
#  [7,]  0.35943833 -0.13525598 -0.54889889 -0.004837937
#  [8,] -0.14311817 -0.75040467  1.69853901  0.019451727
#  [9,]  0.55447651  0.31338764  0.29841157 -0.042622962
# [10,] -0.45909829  0.79915944  0.10847944  0.120741380

Example: Rounding All Values in a Matrix Using the round() Function

x_new <- round(x, 2)        # Applying round() function
x_new
#        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
#  [1,]  0.27  1.17  0.66  0.54 -1.34  0.01 -0.08 -0.09 -1.40  0.33
#  [2,] -0.12 -0.33 -0.73  2.09  0.35 -0.13 -0.55  0.67 -0.07 -1.50
#  [3,] -0.85  0.90 -1.44 -0.28  1.36 -0.79  1.83 -0.56  0.80  0.53
#  [4,]  0.02  0.33  0.45 -0.35 -1.05 -0.29  0.66  0.78 -1.85  0.86
#  [5,]  0.98  0.23 -0.09 -0.96 -0.39 -0.36  0.05 -0.48 -0.46  2.30
#  [6,]  0.73 -0.32 -1.96  0.59 -0.29 -0.96  2.01 -1.46  0.34  0.96
#  [7,]  0.90  0.29  0.80 -0.95  0.56  0.49  0.36 -0.14 -0.55  0.00
#  [8,]  1.33 -0.08 -1.78 -1.95  0.63  0.09 -0.14 -0.75  1.70  0.02
#  [9,] -0.27 -0.34 -0.33 -0.97  0.24 -0.99  0.55  0.31  0.30 -0.04
# [10,] -1.09  0.74 -1.29 -0.69 -0.13  2.75 -0.46  0.80  0.11  0.12

Further Resources

You may find some related R programming tutorials on topics such as indices, missing data, matrices, and extracting data below:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top