Fast (Grouped) Observation Count for Matrix-Like Objects
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.
fNobs(x, ...) ## Default S3 method: fNobs(x, g = NULL, TRA = NULL, use.g.names = TRUE, ...) ## S3 method for class 'matrix' fNobs(x, g = NULL, TRA = NULL, use.g.names = TRUE, drop = TRUE, ...) ## S3 method for class 'data.frame' fNobs(x, g = NULL, TRA = NULL, use.g.names = TRUE, drop = TRUE, ...) ## S3 method for class 'grouped_df' fNobs(x, TRA = NULL, use.g.names = FALSE, keep.group_vars = TRUE, ...)
x |
a vector, matrix, data frame or grouped data frame (class 'grouped_df'). |
g |
a factor, |
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 |
use.g.names |
logical. Make group-names and add to the result as names (default method) or row-names (matrix and data frame methods). No row-names are generated for data.table's. |
drop |
matrix and data.frame method: Logical. |
keep.group_vars |
grouped_df method: Logical. |
... |
arguments to be passed to or from other methods. |
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.
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.
## 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 data frames - created with dplyr::group_by or fgroup_by
library(dplyr)
airquality %>% group_by(Month) %>% fNobs
wlddev %>% group_by(country) %>%
select(PCGDP,LIFEEX,GINI,ODA) %>% fNobsPlease choose more modern alternatives, such as Google Chrome or Mozilla Firefox.