Hello everyone, I am trying to prune a data frame for partial least squares analysis. I need to delete an entire row if one cell in the row contains a NA. Presently, I am running a loop that is supposed to extract the rows that are full of numbers into a second data frame and skips the rows that contain a single NA value. I want to know if there is a simple way to determine if a row (about 20 columns) contains a single NA value without running a loop that checks each individual cell. Thanks in advance. __________________ Payam Minoofar, Ph.D. Scientist Meissner Filtration Products 4181 Calle Tesoro Camarillo, CA 93012 +1 805 388 9911 ext. 159 +1 805 388 5948 fax payam.minoofar at meissner.com -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: ATT00001.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090902/1ca4c741/attachment-0002.txt>
?is.na ?sum ?if ?else I think that is how I would approach it, but I could be far off. On Wed, Sep 2, 2009 at 12:09 PM, Payam Minoofar<payam.minoofar at meissner.com> wrote:> Hello everyone, > > I am trying to prune a data frame for partial least squares analysis. > I need to delete an entire row if one cell in the row contains a NA. > > Presently, I am running a loop that is supposed to extract the rows > that are full of numbers into a second data frame and skips the rows > that contain a single NA value. > > I want to know if there is a simple way to determine if a row (about > 20 columns) contains a single NA value without running a loop that > checks each individual cell. > > Thanks in advance. > > __________________ > Payam Minoofar, Ph.D. > Scientist > Meissner Filtration Products > 4181 Calle Tesoro > Camarillo, CA 93012 > +1 805 388 9911 ext. 159 > +1 805 388 5948 fax > payam.minoofar at meissner.com > > ______________________________________________ > R-help at r-project.org 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. > >-- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
Dear Payam, Here is a suggestion: index <- apply(yourdata, 1, function(x) any( is.na(x) ) ) yourdata[ !index, ] Above creates an index (TRUE) when any row of the data contains a missing value. Then it filters up (extract) the rows that have complete observations. See ?any, ?is.na, ?"!" and ?apply for more information. HTH, Jorge On Wed, Sep 2, 2009 at 1:09 PM, Payam Minoofar <payam.minoofar@meissner.com>wrote:> Hello everyone, > > I am trying to prune a data frame for partial least squares analysis. > I need to delete an entire row if one cell in the row contains a NA. > > Presently, I am running a loop that is supposed to extract the rows > that are full of numbers into a second data frame and skips the rows > that contain a single NA value. > > I want to know if there is a simple way to determine if a row (about > 20 columns) contains a single NA value without running a loop that > checks each individual cell. > > Thanks in advance. > > __________________ > Payam Minoofar, Ph.D. > Scientist > Meissner Filtration Products > 4181 Calle Tesoro > Camarillo, CA 93012 > +1 805 388 9911 ext. 159 > +1 805 388 5948 fax > payam.minoofar@meissner.com > > ______________________________________________ > R-help@r-project.org 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. > >[[alternative HTML version deleted]]
Try this: DF[complete.cases(DF),] On Wed, Sep 2, 2009 at 2:09 PM, Payam Minoofar <payam.minoofar@meissner.com>wrote:> Hello everyone, > > I am trying to prune a data frame for partial least squares analysis. > I need to delete an entire row if one cell in the row contains a NA. > > Presently, I am running a loop that is supposed to extract the rows > that are full of numbers into a second data frame and skips the rows > that contain a single NA value. > > I want to know if there is a simple way to determine if a row (about > 20 columns) contains a single NA value without running a loop that > checks each individual cell. > > Thanks in advance. > > __________________ > Payam Minoofar, Ph.D. > Scientist > Meissner Filtration Products > 4181 Calle Tesoro > Camarillo, CA 93012 > +1 805 388 9911 ext. 159 > +1 805 388 5948 fax > payam.minoofar@meissner.com > > ______________________________________________ > R-help@r-project.org 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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Payam - Take a look at na.omit . This is exactly what it was written for. - Phil On Wed, 2 Sep 2009, Payam Minoofar wrote:> Hello everyone, > > I am trying to prune a data frame for partial least squares analysis. > I need to delete an entire row if one cell in the row contains a NA. > > Presently, I am running a loop that is supposed to extract the rows > that are full of numbers into a second data frame and skips the rows > that contain a single NA value. > > I want to know if there is a simple way to determine if a row (about > 20 columns) contains a single NA value without running a loop that > checks each individual cell. > > Thanks in advance. > > __________________ > Payam Minoofar, Ph.D. > Scientist > Meissner Filtration Products > 4181 Calle Tesoro > Camarillo, CA 93012 > +1 805 388 9911 ext. 159 > +1 805 388 5948 fax > payam.minoofar at meissner.com >
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Payam Minoofar > Sent: Wednesday, September 02, 2009 10:09 AM > To: r-help at r-project.org > Subject: [R] pruning data > > Hello everyone, > > I am trying to prune a data frame for partial least squares > analysis. > I need to delete an entire row if one cell in the row contains a NA. > > Presently, I am running a loop that is supposed to extract the rows > that are full of numbers into a second data frame and skips the rows > that contain a single NA value. > > I want to know if there is a simple way to determine if a row (about > 20 columns) contains a single NA value without running a loop that > checks each individual cell.?na.omit E.g.,> x<-data.frame(one=c(NA,1,2,3), two=c("He","She","It",NA)) > xone two 1 NA He 2 1 She 3 2 It 4 3 <NA>> na.omit(x)one two 2 1 She 3 2 It Many modelling functions have an na.action argument that takes a function like na.omit or na.fail so you don't have to keep an NA-less version of your dataset around.> > Thanks in advance. > > __________________ > Payam Minoofar, Ph.D. > Scientist > Meissner Filtration Products > 4181 Calle Tesoro > Camarillo, CA 93012 > +1 805 388 9911 ext. 159 > +1 805 388 5948 fax > payam.minoofar at meissner.com >