Reputation: 113
I'm looking at the data set pre-loaded into R called UCBAdmissions:
> str(UCBAdmissions)
table [1:2, 1:2, 1:6] 512 313 89 19 353 207 17 8 120 205 ...
- attr(*, "dimnames")=List of 3
..$ Admit : chr [1:2] "Admitted" "Rejected"
..$ Gender: chr [1:2] "Male" "Female"
..$ Dept : chr [1:6] "A" "B" "C" "D" ...
I'd like to create a logical vector of every female where TRUE equates to "Admitted" and FALSE to "Rejected." How do I do that?
Second somewhat related question -- if I wanted to make matrix out of a vector, what would the notation for that be? For lists and data.frame I can use data$variable.I.wish.to.extract.
Jen
Upvotes: 0
Views: 2169
Reputation: 505
If the "with" command isn't working for you, you can also try the following to create your logical vector:
logical.vector <- ifelse(UCBAdmissions$Gender == "Female" & UCBAdmissions$Admit == "Admitted", TRUE, FALSE)
However, this logical vector includes males, meaning that you will get a false for all males in addition to females who were not admitted.
If you want a logical vector that truly returns FALSE only when it is a female who was not admitted, and TRUE when it is a female was was admitted, you will have to first subset the data like so:
females <- subset(UCBAdmissions, Gender == FEMALE)
logical.vector <- ifelse(females$Admit == "Admitted", TRUE, FALSE)
Hope this helps clarify a bit!
Upvotes: 1
Reputation: 174788
For the first question, convert the UCBAdmissions
object, which is a "table"
object to a data frame, and then create a logical:
UCB <- as.data.frame(UCBAdmissions)
ind <- with(UCB, Gender == "Female" & Admit == "Admitted")
which gives
R> ind
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
[13] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
I suspect this *isn't what you want though, as the data are stored in contingency table format; there are only aggregated counts in the various classes. What ind
is indexing is which rows of UCB
where Gender == "Female"
and Admit = "Admitted"
. You'd get the same number of TRUE
and FALSE
values if you asked for Gender == "Female"
and Admit = "Rejected"
, just in a different order.
If you want to blow that out to actual data, you need to repeat the rows of UCB
R> head(UCB)
Admit Gender Dept Freq
1 Admitted Male A 512
2 Rejected Male A 313
3 Admitted Female A 89
4 Rejected Female A 19
5 Admitted Male B 353
6 Rejected Male B 207
Freq
times for each row, and then create the logical vector. Is that what you want?
Upvotes: 1