[R] Problem with indexing (subscript out of bounds)

Nils Trebing nils.trebing at gmx.de
Sun Dec 11 01:00:51 CET 2005


Hi,

I am trying to index the rows of a model matrix where, due to missing
values in the dataset used for the regression, the row numbers sometimes
"jump" like this:

> X[17:19,]
   (Intercept)  PCINIFM
17           1 36.76471
19           1 13.23529
20           1 32.35294

So, the problem is if I select for example X[20, ], I actually get the
21th observation. I've tried to work around this problem by using
X[as.character(20) , ]. Thus I actually get what I want. However, I need
to access sequences of rows, but if there is some missing row, this
doesn't work:

> X[as.character(17:19),]
Error: subscript out of bounds

Is there a way to make R handle this more gracefully, so that, in this
case, it would just return the rows 17 and 19 without complaining?

I apologize in advance for this dumb question of the beginner who I am
and for which I didn't find a solution in the documentation!

Thanks in advance,
Nils




More information about the R-help mailing list