0

I have a list of matrices. I want to round off each element of the matrix in the list. Somehow I am not able to get how to do it. I tried the following way but it doesn't work: e.g.

a<-list(matrix(c(1:12/6),nrow=3),matrix(c(1:12/7),nrow=3),matrix(c(5:16/6),nrow=3))

a<-lapply(a,function(x) round(x,digits=5))

which gives the following output:

[[1]]
                       [,1]                   [,2]                  [,3]
[1,] 0.16667000000000001259 0.66666999999999998483 1.1666700000000000959
[2,] 0.33333000000000001517 0.83333000000000001517 1.3333299999999999041
[3,] 0.50000000000000000000 1.00000000000000000000 1.5000000000000000000
                      [,4]
[1,] 1.6666700000000000959
[2,] 1.8333299999999999041
[3,] 2.0000000000000000000

[[2]]
                      [,1]                  [,2]                  [,3]
[1,] 0.1428599999999999870 0.5714299999999999935 1.0000000000000000000
[2,] 0.2857100000000000195 0.7142899999999999805 1.1428599999999999870
[3,] 0.4285700000000000065 0.8571400000000000130 1.2857099999999999085
                      [,4]
[1,] 1.4285699999999998955
[2,] 1.5714300000000001045
[3,] 1.7142900000000000915

[[3]]
                       [,1]                  [,2]                  [,3]
[1,] 0.83333000000000001517 1.3333299999999999041 1.8333299999999999041
[2,] 1.00000000000000000000 1.5000000000000000000 2.0000000000000000000
[3,] 1.16667000000000009585 1.6666700000000000959 2.1666699999999998738
                      [,4]
[1,] 2.3333300000000001262
[2,] 2.5000000000000000000
[3,] 2.6666699999999998738

With format function it works but it gives each element as character. When I try to convert the character elements into numeric it again returns the numbers up to many decimal places. How can I get each element of the matrices in the list as rounded numeric numbers ?

Pulsar_534511
  • 98
  • 1
  • 13
  • 1
    You can't really have exact decimal roundings on computers. Instead you should adjust your print options so fewer decimal places are printed. – Gregor Thomas Mar 14 '18 at 16:47
  • 1
    The first dupe (Why are these numbers not equal?) explains the "why", the second dupe (Controlling number of decimal digits in print output in R) has nice explanations of controlling the printing behavior. – Gregor Thomas Mar 14 '18 at 16:50
  • I am not getting that output, with the code you have posted I don't see a problem at all. – Rui Barradas Mar 14 '18 at 16:50
  • @RuiBarradas OP must have adjusted their `digits` option up at some point to print this many decimal places. – Gregor Thomas Mar 14 '18 at 16:52
  • @Gregor My `options()$digits` returns `7`. And the OP's code rounds to 5 decimal places as expected. – Rui Barradas Mar 14 '18 at 16:54
  • Thanks for your comment. I am aware of the fact you mentioned. For some computations I want to compare the elements of the matrix and they must be equal. However, the question is equal up to how many decimal places? Rounding them off up to certain digits can help. So I am trying to round them off up to say 5 digits and in the further computations my program should consider the values up to 5 digits only. – Pulsar_534511 Mar 14 '18 at 17:00
  • @RuiBarradas I don't understand your point. You are using the default. OP is not using the default. You get different behavior. – Gregor Thomas Mar 14 '18 at 17:26
  • 1
    @Pulsar_534511 Re-read the "Why are these numbers not equal". *You can't guarantee an exact decimal value*. Using `round()` doesn't make having an exact decimal value with a small number of digits possible *because its impossible*. The answers at that link have several recommendations for testing equality with a tolerance. If you want to make sure that values are equal up to 5 digits of accuracy, then use one of those methods with an appropriate tolerance, like `0.000005`. Or multiply by `1e5`, round to integers, and do an equality comparison. Or one of the other methods recommended there. – Gregor Thomas Mar 14 '18 at 17:29
  • @Gregor Thanks for the explanation! – Pulsar_534511 Mar 15 '18 at 12:39

0 Answers0