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
signcount(DSM_TermTermMatrix) # dense matrixsigncount(DSM_TermContextMatrix) # sparse dgCMatrixsigncount(DSM_TermContextMatrix, "nonneg") # confirm that it is non-negative