I have a column of catch rate data in a DF (df$catch.rate) that contains a combination of decimal values and zeros.
I would like to calculate the percentage of zero rows in the whole column to give me an indication of their contribution to the data.
mean(!df$catch.rate)
will do the trick. You can add the argument na.rm = TRUE
if there are NA
s.
sum(df$catch.rate %in% 0 ) / nrow(df)
I suggest using %in%
if you have NA
values..... e.g.
x <- c(0,NA,1)
sum(x == 0 ) / length(x)
#[1] NA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With