how to delete columns with NA values?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

how to delete columns with NA values?

muting
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?
I search a lot, all I found is how to delete column with all NA values..

Thanks a lot

muting
Reply | Threaded
Open this post in threaded view
|

Re: how to delete columns with NA values?

Erik Iverson-3
Hello,

muting wrote:
> Hi everyone:
>
> I have a dataset:

This looks like a matrix.  To perform functions on each row or column of
a matrix, use the apply function.

If you had a data.frame, you could perform a function on each column
using sapply or lapply.

>
> 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?

## for a data.frame x
x <- data.frame(a = 1:10, b = c(2:10, NA), c = 2:11)

x[sapply(x, function(x) !any(is.na(x)))]

## for a matrix y
y <- matrix(1:30, ncol = 3)
y[20] <- NA

y[, apply(y, 2, function(x) !any(is.na(x)))]

## or maybe even simply...
y[,complete.cases(t(y))]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to delete columns with NA values?

Chuck Cleland
In reply to this post by muting
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to delete columns with NA values?

Stefan Uhmann-2
In reply to this post by muting
Hi muting,

# your data
muting <- data.frame(col1 = c(1,1,2,1,2,1), col2=c(NA,1,2,1,2,NA))
# 1. finding rows with NA
is.na(muting)
# 2. counting the NAs per column
colSums(is.na(muting))
# 3. keeping only the ones without NAs
muting[,colSums(is.na(muting)) == 0]

Regards,
Stefan

schrieb muting, Am 14.04.2010 16:56:

>
> 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?
> I search a lot, all I found is how to delete column with all NA values..
>
> Thanks a lot
>
> muting
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to delete columns with NA values?

muting
In reply to this post by muting
Thank you all!
It works well now
Raj
Reply | Threaded
Open this post in threaded view
|

Re: how to delete columns with NA values?

Raj
This post has NOT been accepted by the mailing list yet.
In reply to this post by Chuck Cleland
Your dataset is :

tm1
col1 col2
[1,] 1 NA
[2,] 1 1
[3,] 2 2
[4,] 1 1
[5,] 2 2
[6,] 1 NA

if you want to  delete entire column 2 that has NA in it(not all of them are NAs)

tm1
col1
[1,] 1
[2,] 1
[3,] 2
[4,] 1
[5,] 2
[6,] 1
if you use this code then uyou will get required result .

tm1[!colSums(tm1)%in%NA]