I have the following framework:
col1 <- c("avi","chi","chi","bov","fox","bov","fox","avi","bov",
"chi","avi","chi","chi","bov","bov","fox","avi","bov","chi")
col2 <- c("low","med","high","high","low","low","med","med","med","high",
"low","low","high","high","med","med","low","low","med")
col3 <- c(0,1,1,1,0,1,0,0,0,0,0,0,1,1,1,1,0,1,0)
test_data <- cbind(col1, col2, col3)
test_data <- as.data.frame(test_data)
And I want to get something like this table (the values are random):
Species Pop.density %Resistance CI_low CI_high Total samples
avi low 2.0 1.2 2.2 30
avi med 0 0 0.5 20
avi high 3.5 2.9 4.2 10
chi low 0.5 0.3 0.7 20
chi med 2.0 1.9 2.1 150
chi high 6.5 6.2 6.6 175
Collision with resistance is based on the col3 value above, where 1 = stable and 0 = unstable. I tried the following:
library(dplyr)
test_data<-test_data %>%
count(col1,col2,col3) %>%
group_by(col1, col2) %>%
mutate(perc_res = prop.table(n)*100)
I tried this and it seems to be almost a trick since I get a percentage of the total 1s and 0s in col3 for each value in col1 and 2, however the resulting samples are wrong, as I count all three columns when the correct count will be only for col1 and 2.
For the confidence interval, I would use the following:
binom.test(resistant samples,total samples)$conf.int*100
However, I'm not sure how to implement it along with the rest. Is there an easy and quick way to do this?