[R] how to delete columns with NA values?

Chuck Cleland ccleland at optonline.net
Wed Apr 14 17:13:14 CEST 2010


On 4/14/2010 10:56 AM, muting wrote:
> Hi everyone:
> 
> I have a dataset:
> 
> tm1
>      col1 col2
> [1,]    1   NA
> [2,]    1    1
> [3,]    2    2
> [4,]    1    1
> [5,]    2    2
> [6,]    1   NA
> 
> I need to delete entire column 2 that has NA in it(not all of them are NAs),
> and the result I want is
> 
> tm1
>      col1 
> [1,]    1  
> [2,]    1    
> [3,]    2    
> [4,]    1    
> [5,]    2    
> [6,]    1   
> 
> what should I do? 

subset(tm1, select=colMeans(is.na(tm1)) == 0)

OR

tm1[,colMeans(is.na(tm1)) == 0]

> I search a lot, all I found is how to delete column with all NA values..
> 
> Thanks a lot
> 
> muting

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list