Joel Fürstenberg-Hägg
2010-Jan-15 11:56 UTC
[R] How to delete matrix rows based on NA frequency?
Hi all, I would like to remove rows from a matrix, based on the frequency of missing values. If there are more than 10 % missing values, the row should be deleted. I use the following to calculate the frequencies, thereby getting a new matrix with the frequencies: freqNA=rowMeans(is.na(exprdata)) But is there a shorter way to remove the rows based on "(1-freqNA)>0.1" than looping through the whole matrix using a for loop? All the best, Joel _________________________________________________________________ Hitta kärleken i vinter! http://dejting.se.msn.com/channel/index.aspx?trackingid=1002952 [[alternative HTML version deleted]]
Joel, try this: # sample matrix m <- matrix(sample(c(1:10, NA),150,replace=T),byrow=T,ncol=15) # nr of missing values per row nacounts <- apply(m, 1, function(x)length(x[is.na(x)])) # new matrix newm <- m[nacounts/ncol(m) < 0.1,] greetings, Remko ------------------------------------------------- Remko Duursma Post-Doctoral Fellow Centre for Plants and the Environment University of Western Sydney Hawkesbury Campus Richmond NSW 2753 Dept of Biological Science Macquarie University North Ryde NSW 2109 Australia Mobile: +61 (0)422 096908 www.remkoduursma.com 2010/1/15 Joel F?rstenberg-H?gg <joel_furstenberg_hagg at hotmail.com>:> > Hi all, > > > > I would like to remove rows from a matrix, based on the frequency of missing values. If there are more than 10 % missing values, the row should be deleted. > > > > I use the following to calculate the frequencies, thereby getting a new matrix with the frequencies: > > > > freqNA=rowMeans(is.na(exprdata)) > > > > But is there a shorter way to remove the rows based on "(1-freqNA)>0.1" than looping through the whole matrix using a for loop? > > > > > > All the best, > > > > Joel > > _________________________________________________________________ > Hitta k?rleken i vinter! > http://dejting.se.msn.com/channel/index.aspx?trackingid=1002952 > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >
Henrique Dallazuanna
2010-Jan-15 12:11 UTC
[R] How to delete matrix rows based on NA frequency?
Try this: m[prop.table(rowSums(is.na(m))) < 0.1,] 2010/1/15 Joel F?rstenberg-H?gg <joel_furstenberg_hagg at hotmail.com>:> > Hi all, > > > > I would like to remove rows from a matrix, based on the frequency of missing values. If there are more than 10 % missing values, the row should be deleted. > > > > I use the following to calculate the frequencies, thereby getting a new matrix with the frequencies: > > > > freqNA=rowMeans(is.na(exprdata)) > > > > But is there a shorter way to remove the rows based on "(1-freqNA)>0.1" than looping through the whole matrix using a for loop? > > > > > > All the best, > > > > Joel > > _________________________________________________________________ > Hitta k?rleken i vinter! > http://dejting.se.msn.com/channel/index.aspx?trackingid=1002952 > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O