[R] Subsets without NA

Agustin Lobo alobo at ija.csic.es
Tue Jan 8 15:11:16 CET 2002


This function removes all rows
with at least one NA:
(x is a mtrix)

> no.na.mat
function(x)
{
        resultado <- x[!is.na(abs(x) %*% rep(1, ncol(x))),  ]
        resultado
}

i.e.:

> mat <- matrix(1:12,ncol=3)
> mat
     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> mat [1,1] <- NA
> mat [2,c(2,3)] <- NA
> mat
     [,1] [,2] [,3]
[1,]   NA    5    9
[2,]    2   NA   NA
[3,]    3    7   11
[4,]    4    8   12
> no.na.mat(mat)
     [,1] [,2] [,3]
[1,]    3    7   11
[2,]    4    8   12
> 

Hope it helps

Agus

Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es


On Tue, 8 Jan 2002, Grum, Mikkel wrote:

> Is there a way of removing all rows with missing values from a data frame?
> I usually use 
> subset(x, var1!="NA") 
> and repeat for each variable.  It would be nice to be able to do it in one
> fell swoop.  Also, surprisingly, it doesn't always work.  Sometimes I'm left
> with an empty set even though not all rows have missing values for the
> variable.
> 
> Cheers,
> mikkel
> 
> 
> 
> 
> 
> Mikkel Grum, PhD
> Genetic Diversity Scientist
> International Plant Genetic Resources Institute (IPGRI)
> Sub-Saharan Africa Group
> ***
> c/o ICRAF
> PO Box 30677 Nairobi, Kenya
> Tel: 254 2 524505 / 524500
> Fax: 254 2 524501 / 524001
> m.grum at cgiar.org
> ipgri-kenya at cgiar.org
> www.ipgri.cgiar.org
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list