# how to delete columns with NA values?

6 messages
Open this post in threaded view
|

## how to delete columns with NA values?

 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
Open this post in threaded view
|

## Re: how to delete columns with NA values?

 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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: how to delete columns with NA values?

 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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: how to delete columns with NA values?

 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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.