Learn R Programming

matrixStats (version 0.12.2)

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

## S3 method for class 'default':
rowCounts(x, value=TRUE, na.rm=FALSE, dim.=dim(x), ...)
  ## S3 method for class 'default':
colCounts(x, value=TRUE, na.rm=FALSE, dim.=dim(x), ...)
  ## S3 method for class 'default':
rowAlls(x, value=TRUE, na.rm=FALSE, dim.=dim(x), ...)
  ## S3 method for class 'default':
colAlls(x, value=TRUE, na.rm=FALSE, dim.=dim(x), ...)
  ## S3 method for class 'default':
rowAnys(x, value=TRUE, na.rm=FALSE, dim.=dim(x), ...)
  ## S3 method for class 'default':
colAnys(x, value=TRUE, na.rm=FALSE, dim.=dim(x), ...)

Arguments

x
An NxK matrix or an N*K vector.
value
A value to search for.
na.rm
If TRUE, NAs are excluded first, otherwise not.
dim.
An integer vector of length two specifying the dimension of x, also when not a matr
...
Not used.

Value

Details

These functions takes either a @matrix or a @vector as input. If a @vector, then argument dim must be specified and fulfill prod(dim) == length(x). The result will be identical to the results obtained when passing matrix(x, nrow=dim[1L], ncol=dim[2L]), but avoids having to temporarily create/allocate a @matrix, if only such is needed only for these calculations.

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