如何将聚合计数转换为意外事件 table?
How to transform aggregated counts into a contingency table?
我想使用多个国家/地区的人口普查数据的数据框,并从中提取 row/column/total 百分比的应急 table。我正在努力解决的问题是数据已经以意外事件 table 的形式汇总。如何将原始计数转换为意外事件 table?
set.seed(1)
country <- c(rep("countryA", 6), rep("countryB", 6))
age <- c(rep(c("0-14", "15-24", "25-59"), 2), rep(c("0-18", "18-30", "30-60"), 2))
sex <- rep(c(rep("female", 3), rep("male", 3)), 2)
count <- abs(round(rnorm(12, 1000000, 500000)))
df <- data.frame(country, age, sex, count)
请注意,在数据中,我有不同的人口普查局报告 MWE 中反映的不同年龄段的人口普查数据。
我想要的是:
[[1]] CountryA
0-14 15-24 25-59
Female row/col/total% row/col/total% row/col/total% Row margins %
Male row/col/total% row/col/total% row/col/total% Row margins %
Column margins % Column margins % Column margins % Total margins %
[[2]] CountryB
0-18 18-30 30-60
Female row/col/total% row/col/total% row/col/total% Row margins %
Male row/col/total% row/col/total% row/col/total% Row margins %
Column margins % Column margins % Column margins % Total margins %
其中row/col/total%应该是可供选择的选项,不一定需要在同一个table中显示。对于最终输出的 class 我也很灵活。我想它是一个列表,但它不一定是。
我知道 prop.table(table(), margins =1/2), janitor::tabyl(), gmodels::CrossTable(), stats::xtabs, Deducer::contingency.tables 以及 http://pcwww.liv.ac.uk/~william/R/crosstab.r。我对这些软件包中的每一个都有的问题是它们将单独的观察作为输入,我无法让它们处理聚合计数。
奖励:除了百分比之外,最好将数据帧重新格式化为 table 计数,这看起来像我显示的计数,但原始数据帧的计数。
一个选项是 split
数据集 'country',用 xtabs
创建摘要 table 并应用 prop.table
lapply(split(df[-1], df$country),
function(x) prop.table(xtabs(count ~ sex + age, droplevels(x))))
也有可能是OP想要的
lapply(split(df[-1], df$country), function(x) {
x1 <- xtabs(count ~ sex + age, droplevels(x))
x2 <- addmargins(x1)
x2[-nrow(x2),-ncol(x2)] <- x2[-nrow(x2),-ncol(x2)]/x2[nrow(x2),
-ncol(x2)]/x2[length(x2)]
x2})
我想使用多个国家/地区的人口普查数据的数据框,并从中提取 row/column/total 百分比的应急 table。我正在努力解决的问题是数据已经以意外事件 table 的形式汇总。如何将原始计数转换为意外事件 table?
set.seed(1)
country <- c(rep("countryA", 6), rep("countryB", 6))
age <- c(rep(c("0-14", "15-24", "25-59"), 2), rep(c("0-18", "18-30", "30-60"), 2))
sex <- rep(c(rep("female", 3), rep("male", 3)), 2)
count <- abs(round(rnorm(12, 1000000, 500000)))
df <- data.frame(country, age, sex, count)
请注意,在数据中,我有不同的人口普查局报告 MWE 中反映的不同年龄段的人口普查数据。
我想要的是:
[[1]] CountryA
0-14 15-24 25-59
Female row/col/total% row/col/total% row/col/total% Row margins %
Male row/col/total% row/col/total% row/col/total% Row margins %
Column margins % Column margins % Column margins % Total margins %
[[2]] CountryB
0-18 18-30 30-60
Female row/col/total% row/col/total% row/col/total% Row margins %
Male row/col/total% row/col/total% row/col/total% Row margins %
Column margins % Column margins % Column margins % Total margins %
其中row/col/total%应该是可供选择的选项,不一定需要在同一个table中显示。对于最终输出的 class 我也很灵活。我想它是一个列表,但它不一定是。
我知道 prop.table(table(), margins =1/2), janitor::tabyl(), gmodels::CrossTable(), stats::xtabs, Deducer::contingency.tables 以及 http://pcwww.liv.ac.uk/~william/R/crosstab.r。我对这些软件包中的每一个都有的问题是它们将单独的观察作为输入,我无法让它们处理聚合计数。
奖励:除了百分比之外,最好将数据帧重新格式化为 table 计数,这看起来像我显示的计数,但原始数据帧的计数。
一个选项是 split
数据集 'country',用 xtabs
创建摘要 table 并应用 prop.table
lapply(split(df[-1], df$country),
function(x) prop.table(xtabs(count ~ sex + age, droplevels(x))))
也有可能是OP想要的
lapply(split(df[-1], df$country), function(x) {
x1 <- xtabs(count ~ sex + age, droplevels(x))
x2 <- addmargins(x1)
x2[-nrow(x2),-ncol(x2)] <- x2[-nrow(x2),-ncol(x2)]/x2[nrow(x2),
-ncol(x2)]/x2[length(x2)]
x2})