如何找出 R 中稀疏矩阵的不同数据类型

How to find out the different data types for sparse matrices in R

中使用 fac2sparse 命令时,我可以在 to 选项中决定条目是否应为 c("d", "i", "l", "n", "z") 类型。帮助只说标准选项是d,代表double。我可以猜到接下来的两个是整数和逻辑,我在某处读到 n 代表模式,但我不知道 z 代表什么。

这应该不需要猜测,所以在帮助文件中哪里可以找到这些信息?我查看了相关的帮助页面和通用矩阵文档,但没有找到。

您可以在 source code

中看到它
if (to != "n") 
    df$x <- rep.int(switch(to, d = 1, i = 1L, l = TRUE, z = 1 + 
        (0+0i)), nrow(df))

所以就像你说的,d 是双精度数,i 是整数,l 是逻辑数,z 是复数,实部为 1,虚部为 0。不过,我不确定您是如何将某些东西从因子转换为复数的。

正如@dww 正确指出的那样,"n" 创建了一个 nsparsematrix,其中只存储了位置信息:

fac2sparse(factor(c(1:3,1:2)),to="l")@x
[1] TRUE TRUE TRUE TRUE TRUE

fac2sparse(factor(c(1:3,1:2)),to="d")@x
[1] 1 1 1 1 1

fac2sparse(factor(c(1:3,1:2)),to="n")@x
Error: no slot of name "x" for this object of class "ngCMatrix"