将R中的整数矩阵保留为整数矩阵 [英] Keep Integer Matrix in R as Integer Matrix
本文介绍了将R中的整数矩阵保留为整数矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在R中有一个用"1"填充的整数矩阵.
Suppose that I have an integer matrix in R filled with "1".
当我将其除以2时,它将转换为双精度矩阵.
When I divide it by two it will be converted to a double matrix.
如何强制R不更改类型矩阵? (因此矩阵将以0代替.5填充.)
How can I force R to not change the type matrix? (So the matrix will be filled with 0 instead of .5).
的确可以稍后再使用round
函数,但这会大大降低速度.
It is true that I can use round
function later, but it decrease the speed significantly.
推荐答案
您可以使用欧几里得除法%/%
,并确保除以2L
,而不是2
.
You can use Euclidian division, %/%
, and make sure you divide by 2L
, not 2
.
x <- matrix(1L, 5, 5)
str( x %/% 2L )
# int [1:5, 1:5] 0 0 0 0 0 0 0 0 0 0 ...
这篇关于将R中的整数矩阵保留为整数矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文