qtab | R Documentation |
A versatile and computationally more efficient replacement for table
. Notably, it also supports tabulations with frequency weights, and computation of a statistic over combinations of variables.
qtab(..., w = NULL, wFUN = NULL, wFUN.args = NULL,
dnn = "auto", sort = .op[["sort"]], na.exclude = TRUE,
drop = FALSE, method = "auto")
qtable(...) # Long-form. Use set_collapse(mask = "table") to replace table()
... |
atomic vectors or factors spanning the table dimensions, (optionally) with tags for the dimension names, or a data frame / list of these. See Examples. |
w |
a single vector to aggregate over the table dimensions e.g. a vector of frequency weights. |
wFUN |
a function used to aggregate |
wFUN.args |
a list of (optional) further arguments passed to |
dnn |
the names of the table dimensions. Either passed directly as a character vector or list (internally
|
sort , na.exclude , drop , method |
arguments passed down to
|
An array of class 'qtab' that inherits from 'table'. Thus all 'table' methods apply to it.
descr
, Summary Statistics, Fast Statistical Functions, Collapse Overview
## Basic use
qtab(iris$Species)
with(mtcars, qtab(vs, am))
qtab(mtcars[.c(vs, am)])
library(magrittr)
iris %$% qtab(Sepal.Length > mean(Sepal.Length), Species)
iris %$% qtab(AMSL = Sepal.Length > mean(Sepal.Length), Species)
## World after 2015
wlda15 <- wlddev |> fsubset(year >= 2015) |> collap(~ iso3c)
# Regions and income levels (country frequency)
wlda15 %$% qtab(region, income)
wlda15 %$% qtab(region, income, dnn = vlabels)
wlda15 %$% qtab(region, income, dnn = "namlab")
# Population (millions)
wlda15 %$% qtab(region, income, w = POP) |> divide_by(1e6)
# Life expectancy (years)
wlda15 %$% qtab(region, income, w = LIFEEX, wFUN = fmean)
# Life expectancy (years), weighted by population
wlda15 %$% qtab(region, income, w = LIFEEX, wFUN = fmean,
wFUN.args = list(w = POP))
# GDP per capita (constant 2010 US$): median
wlda15 %$% qtab(region, income, w = PCGDP, wFUN = fmedian,
wFUN.args = list(na.rm = TRUE))
# GDP per capita (constant 2010 US$): median, weighted by population
wlda15 %$% qtab(region, income, w = PCGDP, wFUN = fmedian,
wFUN.args = list(w = POP))
# Including OECD membership
tab <- wlda15 %$% qtab(region, income, OECD)
tab
# Various 'table' methods
tab |> addmargins()
tab |> marginSums(margin = c("region", "income"))
tab |> proportions()
tab |> proportions(margin = "income")
as.data.frame(tab) |> head(10)
ftable(tab, row.vars = c("region", "OECD"))
# Other options
tab |> fsum(TRA = "%") # Percentage table (on a matrix use fsum.default)
tab %/=% (sum(tab)/100) # Another way (division by reference, preserves integers)
tab
rm(tab, wlda15)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.