Is there a way of removing all rows with missing values from a data frame? I usually use subset(x, var1!="NA") and repeat for each variable. It would be nice to be able to do it in one fell swoop. Also, surprisingly, it doesn't always work. Sometimes I'm left with an empty set even though not all rows have missing values for the variable. Cheers, mikkel Mikkel Grum, PhD Genetic Diversity Scientist International Plant Genetic Resources Institute (IPGRI) Sub-Saharan Africa Group *** c/o ICRAF PO Box 30677 Nairobi, Kenya Tel: 254 2 524505 / 524500 Fax: 254 2 524501 / 524001 m.grum at cgiar.org ipgri-kenya at cgiar.org www.ipgri.cgiar.org -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Grum, Mikkel" <M.GRUM at CGIAR.ORG> writes:> Is there a way of removing all rows with missing values from a data frame? > I usually use > subset(x, var1!="NA") > and repeat for each variable. It would be nice to be able to do it in one > fell swoop. Also, surprisingly, it doesn't always work. Sometimes I'm left > with an empty set even though not all rows have missing values for the > variable.subset(x,complete.cases(x)) or x[complete.cases(x),] BTW: is.na(x) is preferable to x != "NA" -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Grum, Mikkel" wrote:> > Is there a way of removing all rows with missing values from a data frame? > I usually use > subset(x, var1!="NA") > and repeat for each variable. It would be nice to be able to do it in one > fell swoop. Also, surprisingly, it doesn't always work. Sometimes I'm left > with an empty set even though not all rows have missing values for the > variable.See ?na.exclude how to deal with NAs. Uwe -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
try> x.oldV1 V2 V3 V4 1 NA 2 3 4 2 5 6 7 8 3 9 NA 11 12 4 1 2 3 4> x.new_x.old[-c(unique(which(apply(is.na(x.old), FUN=any,MARGIN=1)==TRUE))),]> x.newV1 V2 V3 V4 2 5 6 7 8 4 1 2 3 4 removes a row with at least 1 missing value if you want to take out rows which have all missing values, change the FUN to all Gary __________________________________________________ Gary S. Collins, PhD, Statistics Research Fellow, Quality of Life Unit, European Organisation for Research and Treatment of Cancer, EORTC Data Center, Avenue E. Mounier 83, bte. 11, B-1200 Brussels, Belgium. Tel: +32 2 774 1 606 Fax: +32 2 779 4 568 http://www.eortc.be/home/qol/ __________________________________________________ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This function removes all rows with at least one NA: (x is a mtrix)> no.na.matfunction(x) { resultado <- x[!is.na(abs(x) %*% rep(1, ncol(x))), ] resultado } i.e.:> mat <- matrix(1:12,ncol=3) > mat[,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12> mat [1,1] <- NA > mat [2,c(2,3)] <- NA > mat[,1] [,2] [,3] [1,] NA 5 9 [2,] 2 NA NA [3,] 3 7 11 [4,] 4 8 12> no.na.mat(mat)[,1] [,2] [,3] [1,] 3 7 11 [2,] 4 8 12>Hope it helps Agus Dr. Agustin Lobo Instituto de Ciencias de la Tierra (CSIC) Lluis Sole Sabaris s/n 08028 Barcelona SPAIN tel 34 93409 5410 fax 34 93411 0012 alobo at ija.csic.es On Tue, 8 Jan 2002, Grum, Mikkel wrote:> Is there a way of removing all rows with missing values from a data frame? > I usually use > subset(x, var1!="NA") > and repeat for each variable. It would be nice to be able to do it in one > fell swoop. Also, surprisingly, it doesn't always work. Sometimes I'm left > with an empty set even though not all rows have missing values for the > variable. > > Cheers, > mikkel > > > > > > Mikkel Grum, PhD > Genetic Diversity Scientist > International Plant Genetic Resources Institute (IPGRI) > Sub-Saharan Africa Group > *** > c/o ICRAF > PO Box 30677 Nairobi, Kenya > Tel: 254 2 524505 / 524500 > Fax: 254 2 524501 / 524001 > m.grum at cgiar.org > ipgri-kenya at cgiar.org > www.ipgri.cgiar.org > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I think new <- na.omit(old) should do. `na.omit' returns the object with incomplete cases removed. juli "Grum, Mikkel" ha escrito:> Is there a way of removing all rows with missing values from a data frame? > I usually use > subset(x, var1!="NA") > and repeat for each variable. It would be nice to be able to do it in one > fell swoop. Also, surprisingly, it doesn't always work. Sometimes I'm left > with an empty set even though not all rows have missing values for the > variable. > > Cheers, > mikkel > > Mikkel Grum, PhD > Genetic Diversity Scientist > International Plant Genetic Resources Institute (IPGRI) > Sub-Saharan Africa Group > *** > c/o ICRAF > PO Box 30677 Nairobi, Kenya > Tel: 254 2 524505 / 524500 > Fax: 254 2 524501 / 524001 > m.grum at cgiar.org > ipgri-kenya at cgiar.org > www.ipgri.cgiar.org > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Juli G. Pausas Centro de Estudios Ambientales del Mediterraneo (CEAM) C/ C.R. Darwin 14, Parc Tecnologic, 46980 Paterna, Valencia, SPAIN Tel: (+ 34) 96 131 8227; Fax: (+ 34) 96 131 8190 mailto:juli at ceam.es http://www.gva.es/ceam GCTE Fire Network - http://www.gva.es/ceam/FireNetwork -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Mikkel, A very simple solution is to use na.omit; that is x2 <- na.omit(x) produces a copy of the data frame x with all rows that contain missing data removed. The function na.exclude could be used also. John At 03:09 AM 1/8/2002 -0800, Grum, Mikkel wrote:>Is there a way of removing all rows with missing values from a data frame? >I usually use >subset(x, var1!="NA") >and repeat for each variable. It would be nice to be able to do it in one >fell swoop. Also, surprisingly, it doesn't always work. Sometimes I'm left >with an empty set even though not all rows have missing values for the >variable.----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._