You can compute this using split-apply-combine, using the split
function to break down your data by school, using lapply
to run a function that computes the odds ratio, and using unlist
to combine the results:
unlist(lapply(split(data, data$school), function(x) {
(sum(x$female == 1 & x$pass == 1) / sum(x$female == 1)) /
(sum(x$female == 0 & x$pass == 1) / sum(x$female == 0))
}))
# A school B school C school
# 1.5000000 2.0000000 0.6666667
What I've computed here is actually a risk ratio, since for your dataset the odds ratios are all either infinite or 0.