Skip to contents

With x a matrix containing frequency counts, drop_empty_rc makes a copy of x from which the all-zero rows and all-zero columns are removed. No checks are performed by this function.

Usage

drop_empty_rc(x)

Arguments

x

A matrix, assumed to contain frequency counts.

Value

Matrix, with all-zero rows and columns removed.

Details

This is just a convenience function. It is identical to, and implemented as, x[rowSums(x) > 0, colSums(x) > 0, drop = FALSE].

Examples

# first example
m <- matrix(nrow = 3, byrow = TRUE,
            dimnames = list(c('r1','r2','r3'),
                           c('c1','c2','c3')),
           c(10, 0, 4,
             0, 0, 0,
             5, 0, 7))

m
#>    c1 c2 c3
#> r1 10  0  4
#> r2  0  0  0
#> r3  5  0  7
m2 <- drop_empty_rc(m)
m2
#>    c1 c3
#> r1 10  4
#> r3  5  7

## second example
m <- matrix(nrow = 3, byrow = TRUE,
           dimnames = list(c('r1','r2','r3'),
                          c('c1','c2','c3')),
           c(0, 0, 4,
             0, 0, 0,
             0, 0, 7))
m
#>    c1 c2 c3
#> r1  0  0  4
#> r2  0  0  0
#> r3  0  0  7
m2 <- drop_empty_rc(m)
m2
#>    c3
#> r1  4
#> r3  7

## third example
m <- matrix(nrow = 3, byrow = TRUE,
            dimnames = list(c('r1','r2','r3'),
                            c('c1','c2','c3')),
           c(0, 0, 0,
             0, 0, 0,
             0, 0, 0))
m
#>    c1 c2 c3
#> r1  0  0  0
#> r2  0  0  0
#> r3  0  0  0
m2 <- drop_empty_rc(m)
m2 
#> <0 x 0 matrix>