2

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. 
4

1 に答える 1

1

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

これは画面への出力ですが、latex デバイスへの出力は異なります。

> 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

ここに画像の説明を入力

于 2013-08-16T16:44:27.683 に答える