Learn R Programming

collapse (version 1.1.0)

fNobs: Fast (Grouped) Observation Count for Matrix-Like Objects

Description

fNobs is a generic function that (column-wise) computes the number of non-missing values in x, (optionally) grouped by g. It is much faster than sum(!is.na(x)). The TRA argument can further be used to transform x using its (grouped) observation count.

Usage

fNobs(x, ...)

# S3 method for default fNobs(x, g = NULL, TRA = NULL, use.g.names = TRUE, ...)

# S3 method for matrix fNobs(x, g = NULL, TRA = NULL, use.g.names = TRUE, drop = TRUE, ...)

# S3 method for data.frame fNobs(x, g = NULL, TRA = NULL, use.g.names = TRUE, drop = TRUE, ...)

# S3 method for grouped_df fNobs(x, TRA = NULL, use.g.names = FALSE, keep.group_vars = TRUE, ...)

Arguments

x

a vector, matrix, data.frame or grouped tibble (dplyr::grouped_df).

g

a factor, GRP object, atomic vector (internally converted to factor) or a list of vectors / factors (internally converted to a GRP object) used to group x.

TRA

an integer or quoted operator indicating the transformation to perform: 1 - "replace_fill" | 2 - "replace" | 3 - "-" | 4 - "-+" | 5 - "/" | 6 - "%" | 7 - "+" | 8 - "*" | 9 - "%%" | 10 - "-%%". See TRA.

use.g.names

make group-names and add to the result as names (vector method) or row-names (matrix and data.frame method). No row-names are generated for data.tables and grouped tibbles.

drop

matrix and data.frame method: drop dimensions and return an atomic vector if g = NULL and TRA = NULL.

keep.group_vars

grouped_df method: Logical. FALSE removes grouping variables after computation.

...

arguments to be passed to or from other methods.

Value

Integer. The number of non-missing observations in x, grouped by g, or (if TRA is used) x transformed by its number of non-missing observations, grouped by g.

Details

fNobs preserves all attributes of non-classed vectors / columns, and only the 'label' attribute (if available) of classed vectors / columns (i.e. dates or factors). When applied to data frames and matrices, the row-names are adjusted as necessary.

See Also

fNdistinct, Fast Statistical Functions, Collapse Overview

Examples

Run this code
# NOT RUN {
## default vector method
fNobs(airquality$Solar.R)                   # Simple Nobs
fNobs(airquality$Solar.R, airquality$Month) # Grouped Nobs

## data.frame method
fNobs(airquality)
fNobs(airquality, airquality$Month)
fNobs(wlddev)                               # Works with data of all types!
head(fNobs(wlddev, wlddev$iso3c))

## matrix method
aqm <- qM(airquality)
fNobs(aqm)                                  # Also works for character or logical matrices
fNobs(aqm, airquality$Month)

## method for grouped tibbles - for use with dplyr
library(dplyr)
airquality %>% group_by(Month) %>% fNobs
wlddev %>% group_by(country) %>%
           select(PCGDP,LIFEEX,GINI,ODA) %>% fNobs

# }

Run the code above in your browser using DataLab