1

調査回答の大規模なデータセットから、特定の変数の値を降順に並べ替えたテーブルを作成する必要があります。たとえば、これは私が作成した 1 つのテーブルです。

t8  <- xtabs(meanq8 ~ respondent, data = bfk1)
t13 <- xtabs(meanq13 ~ respondent, data=bfk1)
t18 <- xtabs(meanq18 ~ respondent, data=bfk1)
t23 <- xtabs(meanq23 ~ respondent, data=bfk1)
t28 <- xtabs(meanq28 ~ respondent, data=bfk1)
t33 <- xtabs(meanq33 ~ respondent, data=bfk1)

tab.L6 <- rbind(t8, t13, t18, t23, t28, t33) 
rownames(tab.L6) <- c("I give teachers a sense of overall purpose", "I help clarify the specific meaning of the school's mission in terms of its practical implications for programs and instruction", "I communicate the school mission to staff and students", "I encourage the development of school norms supporting openness to change", "I help teachers understand the relationship between our school's mission and District initiatives", "I work toward whole-staff consensus in establishing priorities for school goals")

これにより、You、Your.Staff、All.Principals、All.Staff の列名を持つ 6x4 double マトリックスが生成されます。ここで、xtable を実行する前に、Your.Staff の値を降順で tab.L6 をソートする必要があります。

私は、plyr と data.table だけでなく、基本の並べ替え関数と順序付け関数を操作しようとしました。次を実行すると

tL6 <- as.data.frame(tab.L6)
table.L6 <- arrange(tL6, desc(Your.Staff))

目的の並べ替えになりますが、行名がありません。

  You Your.Staff All.Principals All.Staff
1   5        5.0            3.8       3.8
2   5        4.5            4.0       3.9
3   5        4.5            3.8       3.6
4   5        4.0            3.5       3.7
5   5        4.0            3.6       3.9
6   4        4.0            3.4       3.7

行名をソートして保持する方法はありますか?

4

1 に答える 1

0

行名にインデックスを付けて、並べ替え後に追加するのはどうですか?

tab.L6 = as.data.frame(matrix(runif(24,1,100),6))
rownames(tab.L6) <- c("I give teachers...", "I help clarify...", "I communicate..", "I encourage...", "I help teachers...", "I work toward...")
names(tab.L6) = c('You','Your.Staff','All.Principals','All.Staff')
tab.L6$index = 1:6
tab.L6

vecnames = c("I give teachers...", "I help clarify...", "I communicate..", "I encourage...", "I help teachers...", "I work toward...")

(t2 = arrange(tab.L6, desc(Your.Staff)))
rownames(t2) = vecnames[t2$index]
t2

出力:

                         You Your.Staff All.Principals All.Staff index
I help teachers... 33.620593  94.174583       12.93681  60.34864     5
I work toward...   98.250119  84.166085       79.57018  89.76993     6
I help clarify...  85.478336  52.664455       71.82487  18.08677     2
I encourage...     55.972257  50.095689       55.96154  60.04199     4
I give teachers... 20.183314  28.812243       21.71087  29.02465     1
I communicate..     8.520616   3.409428       75.94298  56.98706     3

テーブルをエクスポートする前に、インデックス列を削除できます。

t2$index = NULL
于 2012-06-17T15:53:54.533 に答える