Learn R Programming

wordspace (version 0.2-8)

signcount: Efficiently Count Positive, Negative and Zero Values (wordspace)

Description

This function counts the number of positive, negative and zero elements in a numeric vector or matrix, including some types of dense and sparse representations in the Matrix package.

It can be used to test for non-negativity and compute nonzero counts efficiently, without any memory overhead.

Usage

signcount(x, what = c("counts", "nonneg", "nnzero"))

Value

what="counts"

A labelled numeric vector of length 3 with the counts of positive (pos), zero (zero) and negative (neg) values.

what="nonneg"

A single logical value: TRUE if x is non-negative, FALSE otherwise.

what="nonzero"

A single numeric value, the total number of nonzero elements in x.

Arguments

x

a numeric vector or array, or a numeric Matrix (supported formats are dgeMatrix, dgCMatrix and dgRMatrix)

what

whether to return the counts of positive, negative and zero elements (counts), the number of nonzero elements (nnzero), or to test for non-negativity (nonneg); see ‘Value’ below

Author

Stephanie Evert (https://purl.org/stephanie.evert)

Details

x must not contain any undefined values; signcount does not check whether this is actually the case.

Examples

Run this code
signcount(DSM_TermTermMatrix)     # dense matrix
signcount(DSM_TermContextMatrix)  # sparse dgCMatrix
signcount(DSM_TermContextMatrix, "nonneg") # confirm that it is non-negative

Run the code above in your browser using DataLab