- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:根据下面的 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)
}
#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")
#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 ))
关于r - xTable、Sweave、R、交叉表中的计数和百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3444440/
我在 Mac OS X 10.7.3 和 R 2.14.0 上使用 Emacs 24。我有一个文件 foo.Rnw包含 \documentclass[ paper=a4,% 210mm × 297mm
如何缩小 sweave 中的图像尺寸? \documentclass[a4paper]{article} \title{Sweave Example 1} \author{Friedrich Leis
我正在使用 R 和 Sweave 生成报告。 R CMD Sweave MyReport.Rnw 我希望能够向 R 代码发送参数,因为报告当然是 “动态” .所以,我希望能够做这样的事情: R C
我正在尝试通过 Sweave 在我的 LaTeX 文档中加入一些来自 R 的图。 \SweaveOpts{eps = FALSE, pdf = TRUE, echo = FALSE, prefix =
我的问题可能没有标题所暗示的那么笼统。我在 Mac OS X 上使用 MySQL 数据库运行 R 来存储数据。我使用 Komodo/Sciviews-R 已经有一段时间了。最近我需要自动生成的报告并研
我正在编写一个小函数,给出组合条件密度和经验累积分布图。 cdpl>= par(mfrow=c(1,2)) print(cdpl(tre,A,B)) print(cdpl(tre,A,C)) @ Sw
我使用此代码在 Sweave 中制作数字 >= plot(cars) @ \begin{figure} \begin{center} >= > @ \end{center} \caption{Some
我正在尝试编写一份报告,我的问题是每次编译 R 时都会加载我在报告中使用的包(如 ggplot2、MASS、cuature..)。这是非常耗时的。有没有办法查包? 我找到了 cacheSweave 但
我的problem of inserting a pdf graphic with a special character in a Sweave document已通过在 Sweave 本身之外创建
我有一个简短的例程,它使用 gridExtras 包中的 grid.arrange 函数来排列 ggplot 和网格图形。我需要在我的 Sweave 文档中渲染例程的输出(名义上使用 grid.dra
我正在编写一个与 Sweave 一起使用的包,并希望能够从全局环境中对 Sweave 调用进行沙箱处理。这就是我认为它应该工作但没有的方式: sweaveEnv <- new.env() eval(S
我正在使用 cacheSweave带有大型 LaTeX Sweave 文档。而不是放 >= ...snip... @ 在我几乎所有的代码块中,我宁愿有 cache=true成为默认值,并使用 >= .
有时我会制作一个比页面边距更长的 R 代码块(在 Sweave 中)。一旦发生这种情况,有没有办法强制它“转到下一行”? 这是发生这种情况的一个简单示例: \documentclass[a4paper
引用 pgfSweave 手册: How do I set subdirectories for gures and caches? This is straight out of the Sweav
我正在加载 optmatch在 Sweave 文档中,如下所示: >= library(optmatch, quietly=TRUE) @ You're loading optmatch, by Be
我正在使用带有“include.rownames =TRUE”选项的 xtable。如何为行名称添加列标题? 最佳答案 我认为这是不可能的。只需将行名移动到它们自己的列,为列命名,然后使用 inclu
我有一个包含超链接的数据框,我想使用 Sweave 将其显示为可点击的链接。 .我知道 xtable ,但不确定如何使用它来将数据框的内容视为 LaTeX 命令。 最佳答案 一种策略是使用 sanit
我在 sweave 中的 echo 输出宽度有问题,我有一个包含大量文本的列表。问题是来自 R 的回声响应超出了 pdf 中的页面。我试过使用 >= options(width=40) @ 但这并没有
我的当前 Makefile编织看起来像: SUFFIXES: .tex .pdf .Rnw MAIN = lecture INCLUDES = chapter1.tex chapter2.tex ..
我很想在从我的 Sweave 报告创建的所有 graphics.pdf 上使用一些基于 shell 脚本的 pdfcrop。因此,如果所有图形都存储在子文件夹中,而不是放在所有 latex 内容(如
我是一名优秀的程序员,十分优秀!