s-news
[Top] [All Lists]

Re: S-PLUS: na.omit on data.frame columns

To: "'s-news@lists.biostat.wustl.edu'" <s-news@lists.biostat.wustl.edu>
Subject: Re: S-PLUS: na.omit on data.frame columns
From: Bill Osburn <bosburn@sjrwmd.com>
Date: Mon, 2 Feb 2009 14:39:41 -0500
Accept-language: en-US
Acceptlanguage: en-US
In-reply-to: <9E9563D3FAECF9448B370F988FD6BD153A293695E8@MAILSVR.sjrwmd.com>
References: <9E9563D3FAECF9448B370F988FD6BD153A293695E8@MAILSVR.sjrwmd.com>
Thread-index: AcmFbOwNAKzEzW7YQ0i/6hgqh4IIywAACS4w
Thread-topic: S-PLUS: na.omit on data.frame columns

Two nice answers  - Sorry 4

1)

mydf <- mydf[,which(apply(mydf,2, function(x) is.element(NA,x))==F)]

Be aware though that this will transform your data frame into a vector if only one column has no NA values.

 

From: Sebastien Bihorel, PharmD, PhD

 

2)

If all your data (x) are numeric, then try

 

y <- x[,!is.na(colSums(x))]

 

From: Kenton D. Juhlin

 

3)

new.frame<-as.data.frame(lapply(DF, function(x) if(any(is.na(x))) NULL else x))

where DF is the name of your data.frame.

David L Lorenz

 

4)

x[, colSums(is.na(x))==0]

 

and to ensure that you still get a data frame when only one column would be left, use

 

x[, colSums(is.na(x))==0, drop=F]

 

Andreas Krause

 

 

 

 

All worked nicely Thanks

 

<Prev in Thread] Current Thread [Next in Thread>