How can I sum the number of complete cases of two columns?
With c
equal to:
a b
[1,] NA NA
[2,] 1 1
[3,] 1 1
[4,] NA 1
Applying something like
rollapply(c, 2, function(x) sum(complete.cases(x)),fill=NA)
I'd like to get back a single number, 2
in this case. This will be for a large data set with many columns, so I'd like to use rollapply
across the whole set instead of simply doing sum(complete.cases(a,b))
.
Am I over thinking it?
Thanks!
You can calculate the number of complete cases in neighboring matrix columns using rollapply
like this:
m <- matrix(c(NA,1,1,NA,1,1,1,1),ncol=4)
# [,1] [,2] [,3] [,4]
#[1,] NA 1 1 1
#[2,] 1 NA 1 1
library(zoo)
rowSums(rollapply(is.na(t(m)), 2, function(x) !any(x)))
#[1] 0 1 2