I am working with the UCBAdmissions data set, and I want to separate out the data set into the 6 departmental tables that you get when you simply run
>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
I am pretty sure I can make the data set into a dataframe and then go through and grep by department and sum to make tables, but I am wondering if there is an easier way, sine the data is already set up in the exact format I want, I just need to handle each department table individually
Oh, sorry I misread your question. You are not looking for converting this into a data frame but for splitting.
You may use:
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
You can use assign
in a for
loop:
for (i in 1:6){assign(LETTERS[i], UCBAdmissions[,,i])}
A
# Gender
# Admit Male Female
# Admitted 512 89
# Rejected 313 19
and the same goes for B, C, D, E and F
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.