将稀疏矩阵中的所有NaN元素设置为零 [英] Set all NaN elements in sparse matrix to zero

查看:183
本文介绍了将稀疏矩阵中的所有NaN元素设置为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R中Matlab语句X(isnan(X))=0的等效项是什么?注意X在R中的类型为matrix.csr(来自pkg:SparseM.)

What's the equivalent of the Matlab statement X(isnan(X))=0 in R? Note X is of type of matrix.csr in R. (This is from pkg:SparseM.)

推荐答案

确定要使用matrix.csr类吗​​?它来自SparseM软件包,据我所知,至少从软件包文档中可以看出,没有is.na<-is.na[方法. Matrix软件包确实记录了is.na-methods:

Are you sure you want to use the matrix.csr class? It is from the SparseM package and as far as I can tell, at least from the package documentation, there are no is.na<- or is.na[ methods. The Matrix-package does document is.na-methods:

> library(Matrix);M <- Matrix(1:6, nrow=4, ncol=3,
+        dimnames = list(c("a", "b", "c", "d"), c("A", "B", "C")))
> stopifnot(all(!is.na(M)))
> M[2:3,2] <- NA
> M[is.na(M)] <- 0
> M
4 x 3 Matrix of class "dgeMatrix"
  A B C
a 1 5 3
b 2 0 4
c 3 0 5
d 4 2 6

Matrix软件包现在是推荐的软件包之一.我的印象是SparseM没有得到广泛使用.

The Matrix package is now one of the recommended packages. My impression is that SparseM is not in widespread use.

这篇关于将稀疏矩阵中的所有NaN元素设置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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