
On Apr 10, 2012, at 8:01 PM, Worik R wrote:

> Thank you.
>
> That was exactly what I need.
>
> Looking at '?[' I see...
>
>     drop: For matrices and arrays.  If ‘TRUE’ the result is coerced to
>           the lowest possible dimension (see the examples).  This only
>           works for extracting elements, not for the replacement.  See
>           ‘drop’ for further details.
>
>
> But that implies that in the case where 0 rows are returned it  
> should be coerced into zero dimensions.  I am not quite sure what it  
> would mean to be coerced into zero dimensions, but if I read that  
> without having seen the actual behavior (impossible now) I would  
> assume...
>
> M[M[,"a"]==1000,] (from my example below)
>
> would return NULL, which has class "NULL" rather than a matrix with  
> zero rows.

Cue music:
http://www.youtube.com/watch?v=NzlG28B-R8Y

  : "That signpost up ahead  ... You're entering into a land of shadow  
and substance. You just crossedoer into the  R Zen Zone.

...   where the sounds of one hand clapping is the test question:

 > dim(M[0, ])
[1] 0 4
 > str(M[0, ])
  int[0 , 1:4]
  - attr(*, "dimnames")=List of 2
   ..$ : NULL
   ..$ : chr [1:4] "a" "b" "c" "d"
 > dim(M[1, ])
NULL

>
> thanks
> Worik
>
> On Wed, Apr 11, 2012 at 11:54 AM, David Winsemius <dwinsemius@comcast.net 
> > wrote:
>
> On Apr 10, 2012, at 7:33 PM, Worik R wrote:
>
> Friends
>
> I am extracting sub-sets of the rows of a matrix.  Generally the  
> result is
> a matrix.  But there is a special case.  When the result returned is a
> single row it is returned as a vector (in the example below an integer
> vector).  If there are 0, or more than 1 rows returned the result is a
> matrix.
>
> I am doing this in a function and I cannot be sure how many rows I am
> removing.  How can I do this in a general way that always returns a  
> matrix?
>
>
> ?"["
>
> > M[1, , drop=FALSE]
>
>   a b c d
> a1 0 3 2 1
> > class( M[1, , drop=FALSE] )
> [1] "matrix"
>
>
> M <- matrix(0:3, nrow=3, ncol=4)
> colnames(M) <- c('a','b','c','d')
> rownames(M) <- c('a1','b2','c3')
> N <- M[M[,"a"]==0,]
> O <- M[M[,"a"]!=0,]
> P <- M[M[,"a"]==100,]
> c(class(M), class(N), class(O), class(P))
> [1] "matrix"  "integer" "matrix"  "matrix"
> M
>  a b c d
> a1 0 3 2 1
> b2 1 0 3 2
> c3 2 1 0 3
> N
> a b c d
> 0 3 2 1
> O
>  a b c d
> b2 1 0 3 2
> c3 2 1 0 3
> P
>    a b c d
>
>
> cheers
> Worik
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius, MD
> West Hartford, CT
>
>

David Winsemius, MD
West Hartford, CT


	[[alternative HTML version deleted]]

