I am trying to calculate the percentage of different levels of a factor within a group.
I have nested data and would like to see the percentage of schools in each country is a private schools (factor with 2 levels).
However, I cannot figure out how to do that.
# my data:
CNT <- c("A", "A", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C", "C", "D", "D",
"D", "D", "D", "D")
SCHOOL <- c(1:5, 1:3, 1:6, 1:6)
FACTOR <- as.factor(c(1,2,1,2,1,1,1,2,1,2,2,2,1,1,1,1,1,1,1,1))
mydata <- data.frame(CNT, SCHOOL, FACTOR)
head(mydata)
I want a column with the percentage of one level of the Factor (lets say 1) within each country.