# [R] number of pairwise present data in matrix with missings

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 23 15:18:17 CET 2004

```Suppose your matrix is called A (`matrix' is not a good name). Then
crossprod(!is.na(A)) is pretty efficient.  Test:

> A <- matrix(1, 6, 3)
> A[1,1] <- A[3, 1] <- A[2,2] <- NA
> A
[,1] [,2] [,3]
[1,]   NA    1    1
[2,]    1   NA    1
[3,]   NA    1    1
[4,]    1    1    1
[5,]    1    1    1
[6,]    1    1    1
> crossprod(!is.na(A))
[,1] [,2] [,3]
[1,]    4    3    4
[2,]    3    5    5
[3,]    4    5    6

On Tue, 23 Nov 2004, Andreas Wolf wrote:

> is there a smart way of determining the number of pairwise present data
> in a data matrix with missings (maybe as a by-product of some
> statistical function?)
>
> so far, i used several loops like:
>
> for (column1 in 1:99) {
>  for (column2 in 2:100) {
>    for (row in 1:500) {
>      if (!is.na(matrix[row,column1]) & !is.na(matrix[row,column2])) {
>        pairs[col1,col2] <- pairs[col1,col2]+1
>      }
>    }
>  }
> }
>
> but this seems neither the most elegant nor an utterly fast solution.
>
> thanks for suggestions.
> andreas wolf
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```