rowCounts: Counts the number of TRUE values in each row (column) of a...

Description Usage Arguments Value Author(s) Examples

Description

Counts the number of TRUE values in each row (column) of a matrix.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  ## S3 method for class 'matrix'
rowCounts(x, value=TRUE, na.rm=FALSE, ...)
  ## S3 method for class 'matrix'
colCounts(x, value=TRUE, na.rm=FALSE, ...)
  ## S3 method for class 'matrix'
rowAlls(x, value=TRUE, na.rm=FALSE, ...)
  ## S3 method for class 'matrix'
colAlls(x, value=TRUE, na.rm=FALSE, ...)
  ## S3 method for class 'matrix'
rowAnys(x, value=TRUE, na.rm=FALSE, ...)
  ## S3 method for class 'matrix'
colAnys(x, value=TRUE, na.rm=FALSE, ...)

Arguments

x

An NxK matrix.

value

A value to search for.

na.rm

If TRUE, NAs are excluded first, otherwise not.

...

Not used.

Value

rowCounts() (colCounts()) returns an integer vector of length N (K). The other methods returns a logical vector of length N (K).

Author(s)

Henrik Bengtsson

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
x <- matrix(FALSE, nrow=10, ncol=5)
x[3:7,c(2,4)] <- TRUE
x[2:4,] <- TRUE
x[,1] <- TRUE
x[5,] <- FALSE
x[,5] <- FALSE

print(x)

print(rowCounts(x))       # 1 4 4 4 0 3 3 1 1 1
print(colCounts(x))       # 9 5 3 5 0

print(rowAnys(x))
print(which(rowAnys(x)))  # 1  2  3  4  6  7  8  9 10
print(colAnys(x))
print(which(colAnys(x)))  # 1 2 3 4

Example output

       [,1]  [,2]  [,3]  [,4]  [,5]
 [1,]  TRUE FALSE FALSE FALSE FALSE
 [2,]  TRUE  TRUE  TRUE  TRUE FALSE
 [3,]  TRUE  TRUE  TRUE  TRUE FALSE
 [4,]  TRUE  TRUE  TRUE  TRUE FALSE
 [5,] FALSE FALSE FALSE FALSE FALSE
 [6,]  TRUE  TRUE FALSE  TRUE FALSE
 [7,]  TRUE  TRUE FALSE  TRUE FALSE
 [8,]  TRUE FALSE FALSE FALSE FALSE
 [9,]  TRUE FALSE FALSE FALSE FALSE
[10,]  TRUE FALSE FALSE FALSE FALSE
 [1] 1 4 4 4 0 3 3 1 1 1
[1] 9 5 3 5 0
 [1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
[1]  1  2  3  4  6  7  8  9 10
[1]  TRUE  TRUE  TRUE  TRUE FALSE
[1] 1 2 3 4

matrixStats documentation built on May 2, 2019, 4:52 p.m.