gpt4 book ai didi

r - xTable、Sweave、R、交叉表中的计数和百分比

转载 作者:行者123 更新时间:2023-12-03 20:01:34 25 4
gpt4 key购买 nike

编辑:根据下面的 aL3xa 答案,我修改了下面的语法。不完美,但越来越近。我仍然没有找到一种方法让 xtable 接受列或行的\multicolumn{} 参数。 Hmisc 似乎也在幕后处理一些此类任务,但了解那里发生的事情似乎有点困难。有没有人对 Hmisc 中的 latex 功能有经验?

ctab <- function(tab, dec = 2, margin = NULL) {
tab <- as.table(tab)
ptab <- paste(round(prop.table(tab, margin = margin) * 100, dec), "%", sep = "")
res <- matrix(NA, nrow = nrow(tab) , ncol = ncol(tab) * 2, byrow = TRUE)
oddc <- 1:ncol(tab) %% 2 == 1
evenc <- 1:ncol(tab) %% 2 == 0
res[,oddc ] <- tab
res[,evenc ] <- ptab
res <- as.table(res)
colnames(res) <- rep(colnames(tab), each = 2)
rownames(res) <- rownames(tab)
return(res)
}

我想创建一个格式为 LaTeX 输出的表格,其中包含每列或变量的计数和百分比。我还没有找到解决这个问题的现成解决方案,但觉得我必须在某种程度上重新创建轮子。

我已经为直接制表开发了一个解决方案,但正在努力采用交叉制表的方法。

首先是一些示例数据:
#Generate sample data
dow <- sample(1:7, 100, replace=TRUE)
purp <- sample(1:4, 100, replace=TRUE)
dow <- factor(dow, 1:7, c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"))
purp <- factor(purp, 1:4, c("Business", "Commute", "Vacation", "Other"))

现在工作的直接标签功能:
customTable <- function(var, capt = NULL){
counts <- table(var)
percs <- 100 * prop.table(counts)

print(
xtable(
cbind(
Count = counts
, Percent = percs
)
, caption = capt
, digits = c(0,0,2)
)
, caption.placement="top"
)
}

#Usage
customTable(dow, capt="Day of Week")
customTable(purp, capt="Trip Pupose")

有没有人有任何建议将其用于交叉表(即旅行目的的星期几)?这是我目前编写的内容,它不使用 xtable 库并且几乎可以工作,但不是动态的并且很难使用:
#Create table and percentages
a <- table(dow, purp)
b <- round(prop.table(a, 1),2)

#Column bind all of the counts & percentages together, this SHOULD become dynamic in future
d <- cbind( cbind(Count = a[,1],Percent = b[,1])
, cbind(Count = a[,2], Percent = b[,2])
, cbind(Count = a[,3], Percent = b[,3])
, cbind(Count = a[,4], Percent = b[,4])
)

#Ugly function that needs help, or scrapped for something else
crossTab <- function(title){
cat("\\begin{table}[ht]\n")
cat("\\begin{center}\n")
cat("\\caption{", title, "}\n", sep="")

cat("\\begin{tabular}{rllllllll}\n")
cat("\\hline\n")

cat("", cat("", paste("&\\multicolumn{2}{c}{",colnames(a), "}"), sep = ""), "\\\\\n", sep="")
c("&", cat("", colnames(d), "\\\\\n", sep=" & "))
cat("\\hline\n")
c("&", write.table(d, sep = " & ", eol="\\\\\n", quote=FALSE, col.names=FALSE))

cat("\\hline\n")
cat("\\end{tabular}\n")
cat("\\end{center}\n")
cat("\\end{table}\n")
}

crossTab(title = "Day of week BY Trip Purpose")

最佳答案

在 Tables 包中,它是一行:

# data:
dow <- sample(1:7, 100, replace=TRUE)
purp <- sample(1:4, 100, replace=TRUE)
dow <- factor(dow, 1:7, c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"))
purp <- factor(purp, 1:4, c("Business", "Commute", "Vacation", "Other"))

dataframe <- data.frame( dow, purp)

# The packages

library(tables)
library(Hmisc)

# The table
tabular( (Weekday=dow) ~ (Purpose=purp)*(Percent("row")+ 1) ,data=dataframe )

# The latex table
latex( tabular( (Weekday=dow) ~ (Purpose=purp)*(Percent("col")+ 1) ,data=dataframe ))

使用 booktabs,你会得到这个(可以进一步定制):

enter image description here

关于r - xTable、Sweave、R、交叉表中的计数和百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3444440/

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