Calculate the percentage of zeros that I have in a

2020-04-08 11:53发布

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.

标签: r dataframe
4条回答
我欲成王,谁敢阻挡
2楼-- · 2020-04-08 12:29
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
查看更多
smile是对你的礼貌
3楼-- · 2020-04-08 12:32
nrow(df[df$catch.rate == 0,])/nrow(df)
查看更多
家丑人穷心不美
4楼-- · 2020-04-08 12:50
sum(df$catch.rate==0)/length(df$catch.rate)

There's probably a more R-ish way, but this is the quickest I could come up with.

查看更多
Bombasti
5楼-- · 2020-04-08 12:54
mean(!df$catch.rate)

will do the trick. You can add the argument na.rm = TRUE if there are NAs.

查看更多
登录 后发表回答