## 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)
Run the code above in your browser using DataLab