Question

I would like to subset one matrix based on X2 collumn values. I Tryied it:

on <- subset(mat.num, X2 <= -3)
un <- subset(mat.num, X2 >= -1.50000 & X2 <= -0.3599999)
dn <- subset(mat.num, X2 >= -0.3599998 & X2 <= 0.5)

But I get this error:

Error in subset.matrix(mat.num, X2 <= -3) : object 'X2' not found.

ps: I have one X2 column:

mat.num head:

     T_EBV     X2
[1,] 0.09 -0.00777840
[2,] 0.26  0.03600431
[3,] 0.20 -0.06191900
[4,] 0.25  0.13423752
[5,] 0.42  0.06354759
[6,] -0.20  0.06303164
Was it helpful?

Solution

The matrix method doesn't reference column names the same way that you can with data frames. You probably want:

subset(mat.num, mat.num[,2] <= -3)

If you look at the code for subset.matrix you'll see that it's not evaluating the subset criteria inside any special environment:

if (missing(subset)) 
        subset <- TRUE
else if (!is.logical(subset)) 
        stop("'subset' must be logical")
x[subset & !is.na(subset), vars, drop = drop]

as opposed to subset.data.frame which is using eval and substitute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top