Hi all, I have a binary matrix with NAs included. Each row and column includes at least one NA, so I don't want to omit them. Is there a way to sum across rows and columns, ignoring the NAs but not deleting the row or column? If not, I suppose I can write a loop function, but I have learned that it is best to stay away from loops if possible. Thanks for any help, Wade
use: sum(x,na.rm=T) like this: my.row.sums <- apply(my.matrix,1,sum,na.rm=T) If you want to do across columns then use 2 instead of 1. Look at ?apply and ?sum. Adrian On Fri, May 29, 2009 at 7:46 AM, Wade Wall <wade.wall@gmail.com> wrote:> Hi all, > > I have a binary matrix with NAs included. Each row and column > includes at least one NA, so I don't want to omit them. Is there a > way to sum across rows and columns, ignoring the NAs but not deleting > the row or column? If not, I suppose I can write a loop function, but > I have learned that it is best to stay away from loops if possible. > > Thanks for any help, > > Wade > > ______________________________________________ > 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]]
Wade Wall wrote:> Hi all, > > I have a binary matrix with NAs included. Each row and column > includes at least one NA, so I don't want to omit them. Is there a > way to sum across rows and columns, ignoring the NAs but not deleting > the row or column? If not, I suppose I can write a loop function, but > I have learned that it is best to stay away from loops if possible. > > Thanks for any help, > > Wade > > ______________________________________________ > 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. >Hi Wade, The rowSums() and colSums() functions have a "na.rm" argument that you could set to TRUE. Would this solve your problem? Cheers, -- *Luc Villandr?* /Biostatistician McGill University Health Center - Montreal Children's Hospital Research Institute/