This is easy at first glance, but I didn't know how to compute it when I started to work on it. The question is to calculate the odds ratio of pass comparing female with male in each school, and the data is constructed like this:
set.seed(1000)
female = sample(c(0,1),size=20,replace=TRUE)
school = factor(sample(c(0:2),size=20,replace=TRUE),
labels=c("A school","B school","C school"))
school = sort(school)
pass = sample(c(0,1),size=20,replace=TRUE)
data = data.frame(female,school,pass)
Thank you very much!
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.