[R]

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Mar 31 16:56:54 CEST 2003


volkswirt at gmx.net writes:

> How can I remove all rows/collumns from a matrix that fulfill a certain  
> condition? For instance: remove all negative elements from a [n,1]-matrix.  
> I intended to write a function for this aim, but it does not work. Despite  
> that it may help to get my point, when I ad it:  
>   
> kratek<-function(eine)  
> {  
> halt<-0  
> for(index in 1:dim(eine)[1])  
> {  
> if(eine[(index-halt),1]<=0)  
> {  
> eine<-eine[-(index-halt),]  
> halt<-halt+1  
> }  
> }  
> eine  
> }  

Like this?

m <- matrix(rnorm(20),ncol=2)
m[6,1]<-NA
m[-which(m[,1]<=0),]

(Notice that this is not the same as m[m[,1]>0,] as soon as NA's are
involved).


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907



More information about the R-help mailing list