gpt4 book ai didi

R - 创建具有多列和多行的交叉表

转载 作者:行者123 更新时间:2023-12-03 19:36:58 25 4
gpt4 key购买 nike

我习惯了 SPSS,我真的很喜欢在那里使用自定义表格来报告调查数据。如果我能在 R 中做类似的事情,我会很高兴。

我想做的是一个表,它有多个行和列,列百分比和计数(N - 百分比基数)

这是调查数据的示例代码:

set.seed(321)
ID <- seq(1:200)
Age <- sample(c("18-34", "35-59"), 200, replace = TRUE)
Sex <- sample(c("Male", "Female"), 200, replace = TRUE)
TOTAL <- rep(c("TOTAL"), 200)
Edu <- sample(c("Lower", "Middle", "Higher"), 200, replace = TRUE)
PurchaseInt <- sample(c("Definitely yes", "Somewhat yes", "Somewhat not", "Definitely not"),200, replace=TRUE)
Relevance <- sample(c("Definitely fits my needs", "Somewhat fits my needs", "Somewhat does not fit", "Definitely does not fit"),200, replace=TRUE)

DF <- data.frame(ID,TOTAL,Sex,Age,Edu,PurchaseInt,Relevance)
head(DF)
ID TOTAL Sex Age Edu PurchaseInt Relevance
1 1 TOTAL Male 35-59 Lower Definitely yes Somewhat fits my needs
2 2 TOTAL Male 35-59 Higher Somewhat not Definitely does not fit
3 3 TOTAL Male 18-34 Higher Definitely yes Somewhat does not fit
4 4 TOTAL Female 18-34 Lower Somewhat not Definitely does not fit
5 5 TOTAL Female 18-34 Higher Definitely yes Somewhat does not fit
6 6 TOTAL Female 18-34 Higher Definitely not Definitely does not fit

# Simple table, 1 variable by 1 variable, no N (BASE) BAD TABLE :(
prop.table(table(DF$PurchaseInt, DF$Sex),2)

Female Male
Definitely not 0.28 0.30
Definitely yes 0.25 0.28
Somewhat not 0.29 0.24
Somewhat yes 0.17 0.18

我真正想得到的是这样的东西(从 SPSS 完成):

sample table from SPSS

我意识到将计数与 col 百分比相结合可能会更加棘手。对我来说至关重要的是找到在一个表(尤其是多列)中报告多行和多列的可能性,因为这对数据分析有很大帮助。

最佳答案

这有两个部分:首先,您要创建表,接下来要报告它。你按不同的边距分割然后把它放在一个表中,这有点奇怪;我也不确定你是如何得到这些数字的,它们是列百分比吗?如果是这样,我会用你的随机种子得到不同的。

无论如何,这是第 1 部分,它为您提供数据。

# a useful function
table_by <- function(row_var, col_var = NULL) {
# the repeated t() below ensures you have a 4 x 1 matrix
tbl <- if (is.null(col_var)) t(t(table(DF[[row_var]]))) else table(DF[[row_var]], DF[[col_var]])
tbl <- prop.table(tbl, 2)
tbl <- round(tbl, 2) * 100
tbl
}

col12 <- rbind(table_by("PurchaseInt", "Sex"), table_by("Relevance", "Sex"))
col34 <- rbind(table_by("PurchaseInt", "Age"), table_by("Relevance", "Age"))
col56 <- rbind(table_by("PurchaseInt", "Edu"), table_by("Relevance", "Edu"))
percent_rows <- cbind(col12, col34, col56)
whole_table <- cbind(
rbind(table_by("PurchaseInt"), table_by("Relevance")),
percent_rows
)

# should be the data you want
whole_table

对于第二部分,您可以使用我的 huxtable包 - 还有其他的:
library(huxtable)
wt_hux <- as_hux(whole_table, add_colnames = TRUE, add_rownames = TRUE)
number_format(wt_hux)[-2,] <- "%.0f%%"
number_format(wt_hux)[2,] <- "%.0f"
wt_hux[1, 1:2] <- c("", "Total")
wt_hux[2, 1] <- "Total"
wt_hux <- insert_row(wt_hux, c("", "Total", "Sex", "", "Age", "", "Edu", "", ""))
colspan(wt_hux)[1, c(3, 5, 7)] <- c(2, 2, 3)
align(wt_hux)[1, c(3, 5, 7)] <- "center"
wt_hux <- insert_column(wt_hux, c("", "", "Total", "PurchaseInt", "", "", "", "Relevance", "", "", ""))
rowspan(wt_hux)[c(4, 8), 1] <- 4

bottom_border(wt_hux)[c(1, 6, 10), ] <- 1 # for example

# should look roughly the way you want. You can print it to PDF or HTML:
wt_hux

关于R - 创建具有多列和多行的交叉表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47418339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com