簡體   English   中英

具有從R到乳膠的行和多列列名的簡單交叉表

[英]Simple crosstable with row- and multicolumn columnnames from R to latex

我試圖在R中生成一個簡單的交叉表,並在Rstudio中使用knitr將其導出到乳膠中。

我希望該表看起來像一個可發布的表,具有該列中變量的每個類別的行標題,列標題和子標題。 由於我的表具有相同的行和列類別,因此我希望將列級標題替換為數字。 請參見下面的示例:

                                      Profession Mother
 ProfesssionFather                1.          2.            3.
 1. Bla                      frequency   frequency    frequency
 2. blahabblab
 3. blahblahblah

我接近“ xtable”(我無法打印行和列標題,而無法顯示多列標題)和“ tables”包(我無法用數字替換列類別)。

最小示例:

work1 <- paste("LongString", 1:10, sep="")
work2 <- paste("LongString", 1:10, sep="")
t <- table(work1, work2) # making table
t # table with repated row/column names
colnames(t) <- paste(1:10, ".", sep="") # replacing column names with numeric values

xtable(t) # headers are omitted for both rows and columns

work <- data.frame(cbind(work1, work2)) # prepare for use of tabular
tabular((FathersProfession=work1) ~ (MothersProfession=work2), data=work) # have headers, but no way to change column categories from "LongString"x to numeric. 

您需要將tabular函數的輸出分配給命名對象:

 tb <- tabular((FathersProfession=work1) ~ (MothersProfession=work2), data=work)
 str(tb)

顯然,數據在列表中,列名在以下屬性中:

 - attr(*, "colLabels")= chr [1:2, 1:10] "MothersProfession" "LongString1" NA "LongString10" ...

所以

 attr(tb,  "colLabels") <- 
       gsub("LongString", "" , attr(tb,  "colLabels") )

然后,這是輸出到屏幕的內容,但是輸出到乳膠設備的內容將有所不同。

> tb

                   MothersProfession                   
 FathersProfession 1                 10 2 3 4 5 6 7 8 9
 LongString1       1                 0  0 0 0 0 0 0 0 0
 LongString10      0                 1  0 0 0 0 0 0 0 0
 LongString2       0                 0  1 0 0 0 0 0 0 0
 LongString3       0                 0  0 1 0 0 0 0 0 0
 LongString4       0                 0  0 0 1 0 0 0 0 0
 LongString5       0                 0  0 0 0 1 0 0 0 0
 LongString6       0                 0  0 0 0 0 1 0 0 0
 LongString7       0                 0  0 0 0 0 0 1 0 0
 LongString8       0                 0  0 0 0 0 0 0 1 0
 LongString9       0                 0  0 0 0 0 0 0 0 1

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM