0

I'm just trying to calculate a proportion of positive samples "1" for each year. Group by year, then sum(binom == "1") / nrows(binom).

structure(list(year = c(2012L, 2012L, 2011L, 2012L, 2015L, 2008L, 
2013L, 2008L, 2010L, 2018L, 2014L, 2018L, 2012L, 2014L, 2016L, 
2010L, 2015L, 2009L, 2012L, 2008L, 2017L, 2014L, 2008L, 2011L, 
2013L, 2013L, 2015L, 2010L, 2017L, 2012L), binom = c(0, 1, 0, 
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
1, 0, 0, 0, 0, 0)), class = c("data.table", "data.frame"), row.names = c(NA, 
-30L), .internal.selfref = <pointer: 0x0000022eda621ef0>)

I know this isn't difficult but I'm just not seeing something...

bi.count <- wi.hog.test %>% group_by(year) %>% summarise(across(proportion = nrow(binom == "1",) / nrow(binom == "0" | binom == "1",)))
Johnny5ish
  • 295
  • 1
  • 2
  • 12
  • 1
    `proportions(table(d), margin = 1)` or [Calculate the mean by group](https://stackoverflow.com/questions/11562656/calculate-the-mean-by-group) – Henrik Feb 20 '21 at 23:15
  • I'm not sure how this is helpful, you didn't give any description and I'm not trying to find the mean. Id that how my question came across? – Johnny5ish Feb 21 '21 at 00:33
  • 1
    Well, mean of a binary vector _is_ the proportion of 1s. `x = c(0, 1, 1, 0, 0)`; `mean(x)`; `sum(x == 1) / length(x)`. The description(s) is in the linked FAQ. And `proportion(table` also gives you the...proportion. Did you even try it? – Henrik Feb 21 '21 at 00:38
  • yes of course, but this is what I got... ```proportions(table(wi.hog.test$binom), margin = 1) 0 1 1 1 ``` – Johnny5ish Feb 21 '21 at 00:49
  • 1
    Replace `d` with the name of the `dput`ed data frame in your question. – Henrik Feb 21 '21 at 00:51
  • Thank you, I got what I needed with your help using ```bi.count <- wi.hog %>% group_by(year) %>% summarise(prop = mean(binom))``` – Johnny5ish Feb 21 '21 at 01:08

0 Answers0