ez.countif: Conditionally count the occurrence of a single value in data...

View source: R/frame.R

ez.countifR Documentation

Conditionally count the occurrence of a single value in data frame columnwise, or rowwise, or both

Description

count within one or more than one columns/rows, or entire data frame (ie, all columns/rows)

Usage

ez.countif(x, cnd, col = NULL, dim = 3, na.rm = FALSE)

Arguments

x

data frame or vector, if vector, parameters col, dim are ignored

col

column evaluated by eval('dplyr::select()'), single or vector. If NULL, all columns used

dim

1=along row (rowwise), 2=along col (colwse), 3=area, both, grand total (within specified cols/rows)

cond

a string like '.>=3', '.=="M"', 'is.na(.)', 'ifelse()'
must use . to refer to each column of data frame (vectorized)
must be able to be evaluated as TRUE/FALSE (essentially sum up TRUE)
wrapper of mutate_all(funs(cnd)), so the same syntax
Note, may not differentiate 5.0 and 5, ie. 5.0==5 TRUE
> < etc, not meaningful for factors, return NA
na.rm=FALSE to catch NA returned above

Value

returns a data frame, if dim=1/2; a single value if dim=3.
vector input x always outputs a single value.

See Also

ez.countif

Examples

sx = c("F", "F", "F", "M", "M", "M")
ht = c(69, 64, 67, 68, 72, 71)
wt = c(148, 132, 142, 149, 167, 165)
people = data.frame(sx, ht, wt)
ez.countif(people,'.=="M"','wt', dim=2)  # wt 0
ez.countif(people,'.>150', dim=2)   # sx ht wt   NA  0  2
ez.countif(people$wt, '.==165')

jerryzhujian9/ezmisc documentation built on March 9, 2024, 12:44 a.m.