我正在使用UCBAdmissions数据集,我想将数据集分离到您运行时获得的6个部门表中
>UCBAdmissions
, , Dept = A
Gender
Admit Male Female
Admitted 512 89
Rejected 313 19
, , Dept = B
Gender
Admit Male Female
Admitted 353 17
Rejected 207 8
, , Dept = C
Gender
Admit Male Female
Admitted 120 202
Rejected 205 391
, , Dept = D
Gender
Admit Male Female
Admitted 138 131
Rejected 279 244
, , Dept = E
Gender
Admit Male Female
Admitted 53 94
Rejected 138 299
, , Dept = F
Gender
Admit Male Female
Admitted 22 24
Rejected 351 317
我很确定我可以将数据集制作成数据帧,然后通过部门和总和grep来制作表格,但我想知道是否有更简单的方法,正弦数据已经设置在我想要的确切格式,我只需要单独处理每个部门表
答案 0 :(得分:0)
哦,对不起,我误解了你的问题。您不是要将其转换为数据框,而是要进行拆分。
您可以使用:
setNames(lapply(1:dim(UCBAdmissions)[3], function (i) UCBAdmissions[,,i]),
dimnames(UCBAdmissions)[[3]])
#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
答案 1 :(得分:0)
您可以在assign
循环中使用for
:
for (i in 1:6){assign(LETTERS[i], UCBAdmissions[,,i])}
A
# Gender
# Admit Male Female
# Admitted 512 89
# Rejected 313 19
同样适用于B,C,D,E和F