所以,我发现这个非常好的交叉表功能确实具有我想要的所有功能: http://pcwww.liv.ac.uk/~william/R/crosstab.r
此函数的输出是一个列表,使用此示例: http://rstudio-pubs-static.s3.amazonaws.com/6975_c4943349b6174f448104a5513fed59a9.html
现在我希望将此输出显示在数据表中... 这就是我放松它的地方。该列表中包含许多表。 我的计划是创建一个闪亮的小部件,允许我加载数据集并设置参数,结果看起来像一个像样的交叉表。
答案 0 :(得分:0)
假设我们有:
x <- crosstab(Survey, row.vars = c("Age", "Sex"), col.vars = "Health", type = "j")
看起来像:
x
## Health Average Good Poor Sum
## Age Sex
## 0-15 Female 15.38 25.64 25.64 66.67
## Male 7.69 15.38 10.26 33.33
## Sum 23.08 41.03 35.90 100.00
## 16-29 Female 22.22 19.44 11.11 52.78
## Male 19.44 19.44 8.33 47.22
## Sum 41.67 38.89 19.44 100.00
## 30-44 Female 6.45 19.35 12.90 38.71
## Male 22.58 19.35 19.35 61.29
## Sum 29.03 38.71 32.26 100.00
## 45-64 Female 15.15 9.09 27.27 51.52
## Male 9.09 18.18 21.21 48.48
## Sum 24.24 27.27 48.48 100.00
## 65+ Female 15.79 7.89 21.05 44.74
## Male 13.16 26.32 15.79 55.26
## Sum 28.95 34.21 36.84 100.00
并且有一个str
结构,看起来像:
str(x)
## List of 10
## $ row.vars : int [1:2] 1 2
## $ col.vars : int 3
## $ dec.places : num 2
## $ type : chr "joint.pct"
## $ style : chr "wide"
## $ percentages: logi TRUE
## $ addmargins : logi TRUE
## $ subtotals : logi TRUE
## $ table : table [1:5, 1:3, 1:4] 6 8 2 5 6 3 7 7 3 5 ...
## ..- attr(*, "dimnames")=List of 3
## .. ..$ Age : chr [1:5] "0-15" "16-29" "30-44" "45-64" ...
## .. ..$ Sex : chr [1:3] "Female" "Male" "Sum"
## .. ..$ Health: chr [1:4] "Average" "Good" "Poor" "Sum"
## ..- attr(*, "class")= chr [1:2] "table" "array"
## $ crosstab : table [1:5, 1:3, 1:4] 15.38 22.22 6.45 15.15 15.79 ...
## ..- attr(*, "dimnames")=List of 3
## .. ..$ Age : chr [1:5] "0-15" "16-29" "30-44" "45-64" ...
## .. ..$ Sex : chr [1:3] "Female" "Male" "Sum"
## .. ..$ Health: chr [1:4] "Average" "Good" "Poor" "Sum"
## ..- attr(*, "class")= chr [1:2] "table" "array"
## - attr(*, "class")= chr "crosstab"
## NULL
您可以尝试:
library(SOfun)
ftable2dt(x$crosstab)
## Age Sex Average Good Poor Sum
## 1: 0-15 Female 15.384615 25.641026 25.641026 66.66667
## 2: 0-15 Male 7.692308 15.384615 10.256410 33.33333
## 3: 0-15 Sum 23.076923 41.025641 35.897436 100.00000
## 4: 16-29 Female 22.222222 19.444444 11.111111 52.77778
## 5: 16-29 Male 19.444444 19.444444 8.333333 47.22222
## 6: 16-29 Sum 41.666667 38.888889 19.444444 100.00000
## 7: 30-44 Female 6.451613 19.354839 12.903226 38.70968
## 8: 30-44 Male 22.580645 19.354839 19.354839 61.29032
## 9: 30-44 Sum 29.032258 38.709677 32.258065 100.00000
## 10: 45-64 Female 15.151515 9.090909 27.272727 51.51515
## 11: 45-64 Male 9.090909 18.181818 21.212121 48.48485
## 12: 45-64 Sum 24.242424 27.272727 48.484848 100.00000
## 13: 65+ Female 15.789474 7.894737 21.052632 44.73684
## 14: 65+ Male 13.157895 26.315789 15.789474 55.26316
## 15: 65+ Sum 28.947368 34.210526 36.842105 100.00000
(或者,ftable2dt(x$table)
,如果这是你要追踪的数据。