Learn R Programming

matrixStats (version 0.1.5)

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

Description

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

Usage

rowCounts(x, na.rm=FALSE, ...)
  colCounts(x, na.rm=FALSE, ...)
  rowAlls(x, na.rm=FALSE, ...)
  colAlls(x, na.rm=FALSE, ...)
  rowAnys(x, na.rm=FALSE, ...)
  colAnys(x, na.rm=FALSE, ...)

Arguments

x
na.rm
If TRUE, NAs are excluded first, otherwise not.
...
Not used.

Value

Examples

Run this code
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

Run the code above in your browser using DataLab