Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-创建具有多个列和行的交叉表_R - Fatal编程技术网

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

R-创建具有多个列和行的交叉表,r,R,我习惯使用SPSS,我真的很喜欢使用自定义表格来报告调查数据。如果我能在R做一些类似的事情,我会非常高兴 我想做的是一个表,它有多个行和列,列的百分比和计数(N-base表示百分比) 以下是调查数据的示例代码: set.seed(321) ID <- seq(1:200) Age <- sample(c("18-34", "35-59"), 200, replace = TRUE) Sex <- sample(c("Male", "Female"), 200, replace

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

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

以下是调查数据的示例代码:

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
set.seed(321)

ID这有两个部分:首先,要创建表,然后要报告它。你按不同的边距分割,然后把它放在一张桌子上,这有点奇怪;我也不知道你是怎么得到这些数字的,它们是不是意味着列百分比?如果是这样,我会用你的随机种子得到不同的

无论如何,这里是第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

非常感谢你的回答!非常有用。我没见过赫克斯塔布

经过一些修改,我得到了我想要的工作方式。代码如下:

# 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
}

# HERE I also added a table showing counts for demographics
col12 <- rbind(table(DF$TOTAL), table_by("PurchaseInt", "TOTAL"), table_by("Relevance", "TOTAL"))
col34 <- rbind(table(DF$Sex), table_by("PurchaseInt", "Sex"), table_by("Relevance", "Sex"))
col56 <- rbind(table(DF$Age), table_by("PurchaseInt", "Age"), table_by("Relevance", "Age"))
col78 <- rbind(table(DF$Edu), table_by("PurchaseInt", "Edu"), table_by("Relevance", "Edu"))

# should be the data you want
whole_table <- cbind(col12, col34, col56,col78)
whole_table

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(2,3, 7), ] <- 1 # for example

# should look roughly the way you want. You can print it to PDF or HTML:
wt_hux
#一个有用的函数

表_可能类似于
prop.table(ftable(PurchaseInt+Relevance~Sex+Age+Edu,DF),margin=2)*100
,但我不认为是这样,你可以使用
kableExtra
中的分组列和行来实现这一点。
# 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
}

# HERE I also added a table showing counts for demographics
col12 <- rbind(table(DF$TOTAL), table_by("PurchaseInt", "TOTAL"), table_by("Relevance", "TOTAL"))
col34 <- rbind(table(DF$Sex), table_by("PurchaseInt", "Sex"), table_by("Relevance", "Sex"))
col56 <- rbind(table(DF$Age), table_by("PurchaseInt", "Age"), table_by("Relevance", "Age"))
col78 <- rbind(table(DF$Edu), table_by("PurchaseInt", "Edu"), table_by("Relevance", "Edu"))

# should be the data you want
whole_table <- cbind(col12, col34, col56,col78)
whole_table

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(2,3, 7), ] <- 1 # for example

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