我有一个稀疏矩阵,当我用R打开它时,所有应该是0的元素都像'我怎么能把这个点化为0呢?

时间:2020-12-08 04:02:24

I had a sparse matrix, when I open it in R, all elements which should be 0 are dots like ' . ', how can I convert it this dot to number 0? That is converting it to numeric matrix that can be used in following calculation.

我有一个稀疏矩阵,当我用R打开它时,所有应该是0的元素都像'我怎么能把这个点化为0呢?那就是把它转换成数字矩阵,可以在以后的计算中使用。

I know a command to convert all the element which is NA to 0: that is

我知道一个命令将所有的元素(NA)转换为0,也就是

Mydata[is.na(Mydata)]<-0

But I do not know how to convert dot to 0.

但我不知道如何将点化为0。

3 个解决方案

#1


2  

If your matrix is sparse you can convert it to a regular matrix using as.matrix

如果你的矩阵是稀疏的,你可以用asn .matrix把它转换成一个正则矩阵

For example,

例如,

m <- Matrix:::Matrix(0, nrow = 2, ncol = 2, sparse = TRUE)

as.matrix(m)

#2


1  

That's just the way sparse matrices from the Matrix package work. The entries are assumed to be zero but they are not actually explicitly stored as zeros in the internal representation. Only the non-zero entries get stored. That's where the "sparse-ness" comes in. If you extract a particular element from the sparse matrix it comes out as a zero.

这就是矩阵包中的稀疏矩阵的工作方式。这些条目被假定为零,但它们实际上并没有显式地以零的形式存储在内部表示中。只存储非零项。这就是“稀疏”的原因。如果你从稀疏矩阵中提取一个特定的元素,它就会变成0。

>  require(Matrix)
Loading required package: Matrix

Attaching package: ‘Matrix’

The following object is masked from ‘package:SparseM’:

    det

> ?Matrix
> M <- Matrix(0, 3, 2)
> M
3 x 2 sparse Matrix of class "dgCMatrix"

[1,] . .
[2,] . .
[3,] . .
> M[1,1]
[1] 0

(Your follow-on question about a SAS document really makes no sense. The only way to add meaning to this discussion would be to post more code and details.)

(关于SAS文档的后续问题确实没有意义。为这个讨论增加意义的唯一方法是发布更多的代码和细节。

#3


0  

Usually I try to deal with this at import so that I don't have a problem with it later. Most of the import commands have something that identifies NA strings (frequently na.strings).

通常我会尝试在import处理这个问题,这样以后就不会有问题了。大多数导入命令都具有识别NA字符串(通常是NA .strings)的内容。

d <- read.csv("~/path/to/your/file.csv", na.strings = ".")

Then your original presumption works well:

那么你最初的推定是有效的:

d[is.na(d)] <- 0

You also won't have to concern yourself with conversion from character to numeric.

您也不必关心从字符到数字的转换。

#1


2  

If your matrix is sparse you can convert it to a regular matrix using as.matrix

如果你的矩阵是稀疏的,你可以用asn .matrix把它转换成一个正则矩阵

For example,

例如,

m <- Matrix:::Matrix(0, nrow = 2, ncol = 2, sparse = TRUE)

as.matrix(m)

#2


1  

That's just the way sparse matrices from the Matrix package work. The entries are assumed to be zero but they are not actually explicitly stored as zeros in the internal representation. Only the non-zero entries get stored. That's where the "sparse-ness" comes in. If you extract a particular element from the sparse matrix it comes out as a zero.

这就是矩阵包中的稀疏矩阵的工作方式。这些条目被假定为零,但它们实际上并没有显式地以零的形式存储在内部表示中。只存储非零项。这就是“稀疏”的原因。如果你从稀疏矩阵中提取一个特定的元素,它就会变成0。

>  require(Matrix)
Loading required package: Matrix

Attaching package: ‘Matrix’

The following object is masked from ‘package:SparseM’:

    det

> ?Matrix
> M <- Matrix(0, 3, 2)
> M
3 x 2 sparse Matrix of class "dgCMatrix"

[1,] . .
[2,] . .
[3,] . .
> M[1,1]
[1] 0

(Your follow-on question about a SAS document really makes no sense. The only way to add meaning to this discussion would be to post more code and details.)

(关于SAS文档的后续问题确实没有意义。为这个讨论增加意义的唯一方法是发布更多的代码和细节。

#3


0  

Usually I try to deal with this at import so that I don't have a problem with it later. Most of the import commands have something that identifies NA strings (frequently na.strings).

通常我会尝试在import处理这个问题,这样以后就不会有问题了。大多数导入命令都具有识别NA字符串(通常是NA .strings)的内容。

d <- read.csv("~/path/to/your/file.csv", na.strings = ".")

Then your original presumption works well:

那么你最初的推定是有效的:

d[is.na(d)] <- 0

You also won't have to concern yourself with conversion from character to numeric.

您也不必关心从字符到数字的转换。