I have a sparse matrix as follows (resulting from a glmnet::lasso)
sp_mat <- new("dgCMatrix", i = c(0L, 1L, 2L, 3L, 4L, 5L, 7L, 9L, 10L, 11L,
13L, 14L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 28L,
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 39L, 40L, 41L, 42L, 43L,
44L, 45L, 46L, 47L, 49L, 50L, 51L, 52L), p = c(0L, 44L), Dim = c(54L,
1L), Dimnames = list(c("Avar", "Bvar", "Cvar", "m14var", "Dvar", "a26var", "a27var",
"k05var", "b02var", "b03var", "b04var", "b05var", "b06var", "b06var",
"Evar", "Fvar", "Gvar", "a41var", "b01var", "Hvar", "d01var",
"d02var", "d03var", "d04var", "d05var", "d06var", "d07var", "d08var",
"d09var", "d10var", "d11var", "d13var", "c04var", "c05var", "c06var",
"c07var", "c08var", "c09var", "c10var", "c11var", "c12var", "c13var",
"c14var", "c16var", "c17var", "c20var", "c21var", "c22var", "c23var",
"c25var", "h12var", "Ivar", "Jvar", "Kvar"), "s0"), x = c(676.349860381008, 0.0000415982516505796, -0.340580369360737,
2.46714903242688, 0.160264243823403, 0.0833642969185734, 4.0007995406493,
-0.0969035782630975, 0.178346528406363, 1.14886155628414, 0.130340223213916,
3.20266511712085, -3.71593684240651, 0.00000519996597479316,
-0.0192641833629383, -0.00170895248265324, -11.0934095084226,
0.320500528908295, -5.20396508589174, -12.7356151414996, -3.83078700482274,
41181.4262207653, -0.0270280830604277, -0.0492721582439391, -0.00630260482614332,
-0.38526832789847, -0.113757419772693, -0.035812506094549, -0.0464701900255123,
-0.154718592743792, 0.0305041274157989, -0.496893176155956, -0.0510692528769553,
-0.254008558243732, 0.12207111760977, -0.22426734553163, 0.110177476530126,
49.4077380004606, 0.695834880415197, 0.110469954763839, -0.113121348884623,
0.949731767706756, 0.469379688615487, -1.25529553988954), factors = list())
54 x 1 sparse Matrix of class "dgCMatrix"
s0
Avar 676.349860381008
Bvar 0.000041598252
Cvar -0.340580369361
m14var 2.467149032427
Dvar 0.160264243823
a26var 0.083364296919
a27var .
k05var 4.000799540649
b02var .
b03var -0.096903578263
... ...
... ...
... ...
Jvar -1.255295539890
Kvar .
I would simply like to extract all variable names which are not .
. So Avar
until a26var
, but not a27var
. I am not very familiar with sparse matrices and everything I tried gave me unexpected results (such as using sp_mat[[1]]
, names(sp_mat)
etc.).
CodePudding user response:
Would summing by rows and eliminating those that are zero work?
> names(sp_mat[rowSums(sp_mat) != 0, ])
[1] "Avar" "Bvar" "Cvar" "m14var" "Dvar" "a26var" "k05var" "b03var"
[9] "b04var" "b05var" "b06var" "Evar" "Gvar" "a41var" "b01var" "Hvar"
[17] "d01var" "d02var" "d03var" "d05var" "d06var" "d07var" "d09var" "d11var"
[25] "d13var" "c04var" "c05var" "c06var" "c07var" "c08var" "c09var" "c11var"
[33] "c12var" "c13var" "c14var" "c16var" "c17var" "c20var" "c21var" "c22var"
[41] "c25var" "h12var" "Ivar" "Jvar"