计数&xTable、Sweave、R、交叉表中的百分比 [英] Counts & Percentages in xTable, Sweave, R, cross tabulations

查看:11
本文介绍了计数&xTable、Sweave、R、交叉表中的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于 aL3xa 在下面的回答,我在下面修改了他的语法.不完美,但越来越接近.我还没有找到让 xtable 接受列或行的 multicolumn{} 参数的方法.Hmisc 似乎也在幕后处理了其中一些类型的任务,但要了解那里发生的事情似乎有点困难.有人对 Hmisc 中的乳胶功能有经验吗?

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 输出创建一个表格,其中包含每个列或变量的计数和百分比.我还没有找到解决这个问题的现成解决方案,但我觉得我必须在某种程度上重新创建轮子.

I would like to create a table formatted for LaTeX output that contains both the counts and percentages for each column or variable. I have not found a ready made solution to this problem, but feel I must be recreating the wheel to some extent.

我已经为直接制表开发了一个解决方案,但在为交叉制表采用某些东西时遇到了困难.

I have developed a solution for straight tabulations, but am struggling with adopting something for a cross tabulation.

首先是一些示例数据:

#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"))

现在可以使用直接选项卡功能:

And now the working straight tab function:

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 库,几乎可以工作,但不是动态的,而且很难使用:

Does anyone have any suggestions for adopting this for cross tabulations (i.e. day of week BY trip purpose)? Here is what I've currently written, which does NOT use the xtable library and ALMOST works, but is not dynamic and is quite ugly to work with:

#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]
")
    cat("\begin{center}
")
    cat("\caption{", title, "}
", sep="") 

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

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

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

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

推荐答案

我无法弄清楚如何使用 xtable 生成多列标题,但我确实意识到我可以连接我的计数 &百分比到同一列以供打印.不理想,但似乎可以完成工作.这是我写的函数:

I wasn't able to figure out how to generate a multi column header using xtable, but I did realize that i could concatenate my counts & percentages into the same column for printing purposes. Not ideal, but seems to get the job done. Here's the function I've written:

ctab3 <- function(row, col, margin = 1, dec = 2, percs = FALSE, total = FALSE, tex = FALSE, caption = NULL){
    tab <- as.table(table(row,col))
    ptab <- signif(prop.table(tab, margin = margin), dec)

    if (percs){

        z <- matrix(NA, nrow = nrow(tab), ncol = ncol(tab), byrow = TRUE) 
        for (i in 1:ncol(tab)) z[,i] <- paste(tab[,i], ptab[,i], sep = " ")
        rownames(z) <- rownames(tab)
        colnames(z) <- colnames(tab)

        if (margin == 1 & total){
            rowTot <- paste(apply(tab, 1, sum), apply(ptab, 1, sum), sep = " ")
            z <- cbind(z, Total = rowTot)
        } else if (margin == 2 & total) {
            colTot <- paste(apply(tab, 2, sum), apply(ptab, 2, sum), sep = " ")
            z <- rbind(z,Total = colTot)
        }
    } else {
        z <- table(row, col)    
    }
ifelse(tex, return(xtable(z, caption)), return(z))
}

可能不是最终产品,但确实允许在参数方面具有一定的灵活性.在最基本的层面上,它只是 table() 的一个包装器,但也可以生成 LaTeX 格式的输出.这是我最终在 Sweave 文档中使用的内容:

Probably not the final product, but does allow for some flexibility in parameters. At the most basic level, is only a wrapper of table() but can also generate LaTeX formatted output as well. Here is what I ended up using in a Sweave document:

<<echo = FALSE>>=
for (i in 1:ncol(df)){
    print(ctab3(
        col = df[,1]
        , row = df[,i]
        , margin = 2
        , total = TRUE
        , tex = TRUE
        , caption = paste("Dow by", colnames(df[i]), sep = " ")
    ))
}
@

这篇关于计数&amp;xTable、Sweave、R、交叉表中的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆