子设置时,R会四舍五入矩阵中的十进制值 [英] R rounds decimal values in matrix when subsetting

查看:72
本文介绍了子设置时,R会四舍五入矩阵中的十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R中的子集有疑问.说我有以下矩阵:

I have a question about subsetting in R. Say I have the following Matrices:

     Ch1.Amplitude Ch2.Amplitude
[1,]      6968.577      9637.309
[2,]     11903.564     11385.656
[3,]     13503.292      9928.314

     Ch1.Amplitude Ch2.Amplitude
[1,]     11903.564     11385.656
[2,]      2519.582      8042.450
[3,]      9878.749      5899.139

我想将第一个矩阵A的第2行与第二个矩阵B的第1行进行匹配.但是,当我使用

I would like to match row 2 of the first Matrix A with row 1 in the second Matrix B. However, when I access the row with

matrixA[2, , drop=F]

这就是我得到的:

     Ch1.Amplitude Ch2.Amplitude
[1,]      11903.56      11385.66

如您所见,小数点后的第三个数字被切掉了!因此,很自然地,如果我使用match()在矩阵B中查找行,它将返回NA. 但是,当我查询多行时,不会发生这种情况.

As you can see, the third number after the decimal point has been chopped off! So naturally, if I use match() to find the row in Matrix B, it will return NA. However, when I query more than one row, this does not happen.

MatrixA[c(1,2),]
     Ch1.Amplitude Ch2.Amplitude
[1,]      6968.577      9637.309
[2,]     11903.564     11385.656

所以我想这与drop = F有关.发生了什么,我该如何避免?

So I suppose it has something to do with drop=F. What is happening and how can I avoid it?

推荐答案

实际上,这里没有错.这只是一个格式问题.您可以轻松检查值是否受影响:

Actually, there is nothing wrong here. It is simply a formatting issue. You easily check that values are not affected:

> identical(matrixA[2, , drop=F], matrix(c(11903.564, 11385.656), nrow=1)
[1] TRUE

如果要查看更多的小数位,可以使用选项(数字):

If want to see more decimal places you can for example use options(digits):

> options(digits=10)
> matrixA[2, , drop=F]
      [,1]      [,2]
[1,] 11903.564 11385.656

或格式:

> format(matrixA[2, , drop=F], nsmall=3)
     [,1]        [,2]       
[1,] "11903.564" "11385.656"

这篇关于子设置时,R会四舍五入矩阵中的十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆