闪亮 - 生成crosstable,列总数和行总数为第1行/列

时间:2016-11-11 17:15:59

标签: r shiny data.table percentage crosstab

我想为行和列生成一个包含总列的crosstable。我尝试使用gmodels包生成crosstable。输出的外观优于普通表函数。表的外观很重要,因为最后必须使用Shiny来显示。但问题是我在行和列的末尾得到列总数和行总数。如何将总列作为表中的第1列和第1行。

以下是我的数据样本。

Location <- sample(c("location A","location B","location C","location D","location E"),20,replace = T) 
Brand <- sample(c("Brand A","Brand B","Brand C"),20,replace = T) 
Year <- rep(c("Year 2014","Year 2015"),10)
Q1 <- sample(1:5,20,replace = T)
Q2 <- sample(1:5,20,replace = T)

mydata <- as.data.table(cbind(Location,Brand,Year,Q1,Q2))

数据量巨大,因此它是data.table。

我用于生成交叉表的代码是 -

library("gmodels")

mydata[,CrossTable(Location,Brand,prop.c = T,prop.r = F,prop.t = F,prop.chisq = F,chisq = F,format = "SPSS")]

这给出了输出,但总列位于列的行末尾。总列中也缺少列%。如何将总列作为第1行和第1列并且还有%?

建议出路。

2 个答案:

答案 0 :(得分:0)

也许这样的事情会发生什么?

myCT <- function(mydata) {
  mydata_ct_n <- dcast.data.table(mydata, Location ~ Brand, margins = T)
  mydata_ct_n[, all := rowSums(.SD), by = Location]
  mydata_ct_n <- rbind(mydata_ct_n[, lapply(.SD, sum), .SDcols = 2:ncol(mydata_ct_n)], mydata_ct_n, fill = T)
  mydata_ct_n$Location[1] <- "all"
  foocols <- c("all", "Location")
  setcolorder(mydata_ct_n, c(foocols, setdiff(colnames(mydata_ct_n), foocols)))

  mydata_ct_p <- copy(mydata_ct_n)
  for (j in 3:ncol(mydata_ct_p)) {
    set(mydata_ct_p, j = j, value = as.numeric(mydata_ct_p[[j]]))
    set(mydata_ct_p, i = 2:nrow(mydata_ct_p), j = j, value = round(100 * mydata_ct_p[2:nrow(mydata_ct_p), j, with = F] / mydata_ct_p[[j]][1], 0))
  }
  set(mydata_ct_p, 1L, 3L:ncol(mydata_ct_p), round(100 * mydata_ct_p[1L, 3L:ncol(mydata_ct_p), with = F] / mydata_ct_p[["all"]][1], 0))

  for (j in 3:ncol(mydata_ct_p)) {
    set(mydata_ct_p, j = j, value = as.character(mydata_ct_p[[j]]))
    set(mydata_ct_n, j = j, value = as.character(mydata_ct_n[[j]]))
    set(mydata_ct_p, j = j, 
        value = paste0(mydata_ct_p[[j]], "% (", mydata_ct_n[[j]], ")"))
  }
  return(mydata_ct_p)
}

Location <- sample(c("location A","location B","location C","location D","location E"),20,replace = T)
Brand <- sample(c("Brand A","Brand B","Brand C"),20,replace = T)
Year <- rep(c("Year 2014","Year 2015"),10)
Q1 <- sample(1:5,20,replace = T)
Q2 <- sample(1:5,20,replace = T)
mydata <- as.data.table(cbind(Location,Brand,Year,Q1,Q2))

out <- myCT(mydata)
print(out)
#    all   Location Brand A Brand B Brand C
# 1:  20        all 30% (6) 35% (7) 35% (7)
# 2:   3 location A  0% (0) 43% (3)  0% (0)
# 3:   5 location B 33% (2) 14% (1) 29% (2)
# 4:   5 location C 50% (3)  0% (0) 29% (2)
# 5:   4 location D 17% (1) 29% (2) 14% (1)
# 6:   3 location E  0% (0) 14% (1) 29% (2)

答案 1 :(得分:0)

您是否尝试过使用sjPlot包....它有一个非常好的功能,sjt.xtab可以生成类似于您要查找的交叉表(列联表)。它有很多选择可供探索。我在下面使用了它们中的一些。您可以查看?sjt.xtab并查看其他可用选项。下面的代码生成具有列百分比的表输出,并且具有总列数和行数。

sjt.xtab(mydata$Location, mydata$Brand,
         show.col.prc = T,
         show.summary = F,
         show.na = F,
         wrap.labels = 50,
         tdcol.col = "#f90470",
         emph.total = T,
         emph.color = "#3aaee5",
         use.viewer = T,
         CSS = list(css.table = "border: 1px solid;",
                    css.tdata = "border: 1px solid;"))