[R] How to delete matrix rows based on NA frequency?

Remko Duursma remkoduursma at gmail.com
Fri Jan 15 13:05:01 CET 2010


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



More information about the R-help mailing list