[R] Matrix manipulation

William Dunlap wdunlap at tibco.com
Thu Jun 11 23:13:08 CEST 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius
> Sent: Thursday, June 11, 2009 1:49 PM
> To: Payam Minoofar
> Cc: r-help at r-project.org
> Subject: Re: [R] Matrix manipulation
> 
> 
> On Jun 11, 2009, at 2:53 PM, Payam Minoofar wrote:
> 
> > Hello everyone,
> >
> > I have a couple of fairly simple questions (I hope) the answers to  
> > which I cannot find through the documentation at the moment.
> >
> >
> > 1.  I would like to delete the a row from a matrix if a certain  
> > elimination criterion is met. I am familiar with x <- x[-7,] (to  
> > remove row 7, for example). Are there any other means of 
> removing an  
> > entire row?
> 
> ?which   # useful for converting logical vectors into argument for  
> functions that require numerics
> 
> M10 <- matrix(1:100, nrow = 10)
> 
> # find row with 63
> which( sapply( 1:10, function(x) 63 %in% M10[x, ]) )
> [1] 3
> 
> M10[-which( sapply( 1:10, function(x) 63 %in% M10[x, ]) ), ]   #  
> remove row with 63
> 
>        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>   [1,]    1   11   21   31   41   51   61   71   81    91
>   [2,]    2   12   22   32   42   52   62   72   82    92
>   [3,]    4   14   24   34   44   54   64   74   84    94
>   [4,]    5   15   25   35   45   55   65   75   85    95
>   [5,]    6   16   26   36   46   56   66   76   86    96
>   [6,]    7   17   27   37   47   57   67   77   87    97
>   [7,]    8   18   28   38   48   58   68   78   88    98
>   [8,]    9   19   29   39   49   59   69   79   89    99
>   [9,]   10   20   30   40   50   60   70   80   90   100

which() is dangerous here.  E.g., if you wanted to use that
idiom to delete any row containing 666 you would get
a 0-row by 10-column matrix, not the expected copy of
the input matrix
  > M10[-which( sapply( 1:10, function(x) 666 %in% M10[x, ]) ), ]
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
(This happens because -integer(0) is no different than integer(0):
both are 0-long vectors.)

I think you should use logical subscripts unless you are really
pressed for space.
    M10[ !sapply(1:10, function(x)666 %in% M10[x,]), ]
When you read '[', say 'such that'.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 

> 
> >  2.  Is there a single command that will rename the index of each  
> > row to match the row number once a row has been deleted. For  
> > example, when row 7 is deleted above, the old row 8 is now row 7,  
> > but the row name is still "8". I have figured out how to assign a  
> > sequence vector to the row names, but I am wondering if there is a  
> > built-in command that does the same thing. (I.e., change 
> the name of  
> > row 7 to "7" from "8".)
> >
> > Thank you very much.
> >
> > Payam
> > --
> > Payam Minoofar, Ph.D.
> > Scientist
> > Meissner Filtration Products
> > 4181 Calle Tesoro
> > Camarillo, CA 93012
> > USA
> > +1 805 388 9911
> > +1 805 388 5948 fax
> > Payam.minoofar at meissner.com
> >
> >
> > 	[[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at 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
> Heritage Laboratories
> West Hartford, CT
> 
> ______________________________________________
> R-help at 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.
> 




More information about the R-help mailing list