如何使用强制转换功能从R数据框中查找均值列联表?
列联表考虑了两个类别变量的数值。通常,我们需要列联表进行计数,尤其是在非参数分析中,但是我们也可能希望使用均值进行分析。因此,我们可以使用reshape包中的cast函数,轻松解决了创建列联表的问题。
请看以下数据帧-
示例
set.seed(99) x1<-rep(c("A","B","C"),times=c(8,7,5)) x2<-sample(c("Low","Medium","High"),20,replace=TRUE) x3<-sample(1:9,20,replace=TRUE) df1<-data.frame(x1,x2,x3) df1
输出结果
x1 x2 x3 1 A Low 2 2 A Medium 7 3 A Medium 6 4 A Low 5 5 A High 8 6 A Medium 7 7 A Medium 6 8 A Medium 5 9 B Medium 1 10 B Low 6 11 B High 6 12 B Low 7 13 B Medium 4 14 B Medium 5 15 B Medium 7 16 C Low 1 17 C Medium 6 18 C Medium 9 19 C Low 9 20 C High 3
使用x1和x2的组合加载重塑包装并为x3创建列联表-
示例
library("reshape") cast(df1,x1~x2,mean,value="x3")
输出结果
x1 High Low Medium 1 A 8 3.5 6.20 2 B 6 6.5 4.25 3 C 3 5.0 7.50
让我们看另一个例子-
示例
y1<-rep(c("Male","Female"),each=10) y2<-sample(c("Weekly","Monthly","Quarterly","Yearly"),20,replace=TRUE) y3<-sample(101:110,20,replace=TRUE) df2<-data.frame(y1,y2,y3) df2
输出结果
y1 y2 y3 1 Male Weekly 102 2 Male Yearly 107 3 Male Quarterly 110 4 Male Yearly 103 5 Male Quarterly 110 6 Male Monthly 110 7 Male Quarterly 105 8 Male Yearly 108 9 Male Quarterly 105 10 Male Quarterly 104 11 Female Quarterly 101 12 Female Monthly 108 13 Female Quarterly 103 14 Female Monthly 107 15 Female Weekly 109 16 Female Yearly 103 17 Female Quarterly 103 18 Female Quarterly 108 19 Female Quarterly 105 20 Female Weekly 105
使用y1和y2的组合为y3创建列联表-
cast(df2,y1~y2,mean,value="y3") y1 Monthly Quarterly Weekly Yearly 1 Female 107.5 104.0 107 103 2 Male 110.0 106.8 102 106