I wanted to calculate counts of different subsets, which worked fine by using:
count.sur <- aggregate(Survived ~ Child + Sex, test.titanic, FUN = length)
### Child Sex Survived
# 1 0 female 142
# 2 1 female 10
# 3 0 male 254
# 4 1 male 12
Then I wanted to calculate the proportions of those counts.
I used this function and added it to my line of code:
prop.fnc <- function(x){
results <- count.sur[,3]/sum(count.sur[,3])
return <- results
}
aggregate(Survived ~ Child + Sex, test.titanic, prop.fnc)
The values returned were all correct but instead of being organized down a column, they were organized going across the rows and replicated themselves 4 times.
# Child Sex Survived.1 Survived.2 Survived.3 Survived.4
# 1 0 female 0.33971292 0.02392344 0.60765550 0.02870813
# 2 1 female 0.33971292 0.02392344 0.60765550 0.02870813
# 3 0 male 0.33971292 0.02392344 0.60765550 0.02870813
# 4 1 male 0.33971292 0.02392344 0.60765550 0.02870813
I'm not sure where I went wrong with the output's format.