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, ...)

# 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 data frame (class '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

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. TRUE drops dimensions and returns 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.

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.

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.

See also

Examples

## default vector method fNobs(airquality$Solar.R) # Simple Nobs
#> [1] 146
fNobs(airquality$Solar.R, airquality$Month) # Grouped Nobs
#> 5 6 7 8 9 #> 27 30 31 28 30
## data.frame method fNobs(airquality)
#> Ozone Solar.R Wind Temp Month Day #> 116 146 153 153 153 153
fNobs(airquality, airquality$Month)
#> Ozone Solar.R Wind Temp Month Day #> 5 26 27 31 31 31 31 #> 6 9 30 30 30 30 30 #> 7 26 31 31 31 31 31 #> 8 26 28 31 31 31 31 #> 9 29 30 30 30 30 30
fNobs(wlddev) # Works with data of all types!
#> country iso3c date year decade region income OECD PCGDP LIFEEX #> 12744 12744 12744 12744 12744 12744 12744 12744 8995 11068 #> GINI ODA #> 1356 8336
head(fNobs(wlddev, wlddev$iso3c))
#> country iso3c date year decade region income OECD PCGDP LIFEEX GINI ODA #> ABW 59 59 59 59 59 59 59 59 32 57 0 20 #> AFG 59 59 59 59 59 59 59 59 16 57 0 58 #> AGO 59 59 59 59 59 59 59 59 38 57 2 56 #> ALB 59 59 59 59 59 59 59 59 38 57 5 30 #> AND 59 59 59 59 59 59 59 59 48 0 0 0 #> ARE 59 59 59 59 59 59 59 59 43 57 0 45
## matrix method aqm <- qM(airquality) fNobs(aqm) # Also works for character or logical matrices
#> Ozone Solar.R Wind Temp Month Day #> 116 146 153 153 153 153
fNobs(aqm, airquality$Month)
#> Ozone Solar.R Wind Temp Month Day #> 5 26 27 31 31 31 31 #> 6 9 30 30 30 30 30 #> 7 26 31 31 31 31 31 #> 8 26 28 31 31 31 31 #> 9 29 30 30 30 30 30
## method for grouped data frames - created with dplyr::group_by or fgroup_by library(dplyr) airquality %>% group_by(Month) %>% fNobs
#> # A tibble: 5 x 6 #> Month Ozone Solar.R Wind Temp Day #> <int> <int> <int> <int> <int> <int> #> 1 5 26 27 31 31 31 #> 2 6 9 30 30 30 30 #> 3 7 26 31 31 31 31 #> 4 8 26 28 31 31 31 #> 5 9 29 30 30 30 30
wlddev %>% group_by(country) %>% select(PCGDP,LIFEEX,GINI,ODA) %>% fNobs
#> Adding missing grouping variables: `country`
#> # A tibble: 216 x 5 #> country PCGDP LIFEEX GINI ODA #> <chr> <int> <int> <int> <int> #> 1 Afghanistan 16 57 0 58 #> 2 Albania 38 57 5 30 #> 3 Algeria 58 57 3 58 #> 4 American Samoa 16 0 0 0 #> 5 Andorra 48 0 0 0 #> 6 Angola 38 57 2 56 #> 7 Antigua and Barbuda 41 57 0 45 #> 8 Argentina 58 57 28 58 #> 9 Armenia 28 57 17 27 #> 10 Aruba 32 57 0 20 #> # ... with 206 more rows