我的问题是分析R中的数据集'UCBAmissions'。我的函数采用2 x 2表并对其进行分析。我已经使用以下代码对部门之间汇总的数据进行了分析:apply(UCBAdmissions,c(1,2), sum)
。我的尝试是在单个部门(A,B,C,D,E,F)中汇总多个2 x 2表。如何制作或排序数据以具有单个汇总表。
#I have tried using sweep in 'R' by my hint. Here is my code:
sweep(UCBAdmissions,c(2,3),apply(UCBAdmissions,c(2,3),sum),"/")
#I just don't want this to be prob.
似乎您正在寻找基本的asplit
和list2env
函数,以按数组的第3维将其拆分为Global环境中的各个表:
A <- asplit(UCBAdmissions, 3)
输出:
> A
$A
Gender
Admit Male Female
Admitted 512 89
Rejected 313 19
$B
Gender
Admit Male Female
Admitted 353 17
Rejected 207 8
$C
Gender
Admit Male Female
Admitted 120 202
Rejected 205 391
$D
Gender
Admit Male Female
Admitted 138 131
Rejected 279 244
$E
Gender
Admit Male Female
Admitted 53 94
Rejected 138 299
$F
Gender
Admit Male Female
Admitted 22 24
Rejected 351 317
再一步获得单个表:
list2env(A,envir = .GlobalEnv)
显示在全局环境中创建的表:
> B
Gender
Admit Male Female
Admitted 353 17
Rejected 207 8