计算我在一列中拥有零的百分比 [英] Calculate the percentage of zeros that I have in a column

查看:64
本文介绍了计算我在一列中拥有零的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DF(df $ catch.rate)中有一列捕获率数据,其中包含十进制值和零的组合。

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.

推荐答案

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

如果您有 NA 值,我建议使用%in% ...例如

I suggest using %in% if you have NA values..... e.g.

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

这篇关于计算我在一列中拥有零的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆