Carl Witthoft
2013-Oct-17 11:34 UTC
[R] How would i sum the number of NA's in multiple vectors
mattbju2013 wrote> Hi guys this is my first post, i need help summing the number of NA's in a > few vectors > > for example.. > > c1<-c(1,2,NA,3,4) > c2<-c(NA,1,2,3,4) > c3<-c(NA,1,2,3,4) > > how would i get a result that only sums the number of NA's in the vector? > the.result.i.want<-c(2,0,1,0,0)See ?is.na . Now, if I can interpret your question correctly, you're actually looking for the number of NA per *position* in the vectors, so let's make them into a matrix first. cmat<-rbind(c1,c2,c3) then use apply over columns apply(cmat,2,function(k)sum(is.na(k))) -- View this message in context: http://r.789695.n4.nabble.com/How-would-i-sum-the-number-of-NA-s-in-multiple-vectors-tp4678411p4678432.html Sent from the R help mailing list archive at Nabble.com.
Joshua Wiley
2013-Oct-17 11:44 UTC
[R] How would i sum the number of NA's in multiple vectors
Or faster (both computational speed and amount of code): colSums(is.na(rbind(c1, c2, c3))) On Thu, Oct 17, 2013 at 4:34 AM, Carl Witthoft <carl@witthoft.com> wrote:> mattbju2013 wrote > > Hi guys this is my first post, i need help summing the number of NA's in > a > > few vectors > > > > for example.. > > > > c1<-c(1,2,NA,3,4) > > c2<-c(NA,1,2,3,4) > > c3<-c(NA,1,2,3,4) > > > > how would i get a result that only sums the number of NA's in the vector? > > the.result.i.want<-c(2,0,1,0,0) > > See ?is.na . > Now, if I can interpret your question correctly, you're actually looking > for > the number of NA per *position* in the vectors, so let's make them into a > matrix first. > > cmat<-rbind(c1,c2,c3) > then use apply over columns > apply(cmat,2,function(k)sum(is.na(k))) > > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/How-would-i-sum-the-number-of-NA-s-in-multiple-vectors-tp4678411p4678432.html > Sent from the R help mailing list archive at Nabble.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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://joshuawiley.com/ Senior Analyst - Elkhart Group Ltd. http://elkhartgroup.com [[alternative HTML version deleted]]