Calculate the percentage of zeros I have in a column

I have a DF catch rate data column (df $ catch.rate) that contains a combination of decimal values ​​and zeros.

I would like to calculate the percentage of null rows in the entire column to give me an idea of ​​their contribution to the data.

+4


source to share


4 answers


sum(df$catch.rate %in% 0 ) / nrow(df)

      

I suggest using %in%

if you have values NA

..... for example



x <- c(0,NA,1) 
sum(x == 0 ) / length(x)
#[1] NA

      

+3


source


mean(!df$catch.rate)

      



will do the trick. You can add an argument na.rm = TRUE

if there is NA

s.

+9


source


nrow(df[df$catch.rate == 0,])/nrow(df)

      

+2


source


sum(df$catch.rate==0)/length(df$catch.rate)

      

There is probably a more R-ish way, but this is the fastest I could think of with.

+1


source







All Articles