R:矩阵列表中每个元素的舍入值

时间:2018-03-14 16:46:01

标签: r list matrix rounding

我有一个矩阵列表。我想整理列表中矩阵的每个元素。不知何故,我无法做到这一点。我尝试了以下方法,但它不起作用: 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))

给出以下输出:

[[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

使用format函数可以正常工作,但它会将每个元素作为字符。当我尝试将字符元素转换为数字时,它再次返回多个小数位数。如何将列表中矩阵的每个元素作为舍入数字数字?

0 个答案:

没有答案