如何使用 cast 函数从 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
加载 reshape 包并使用 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
广告