如何在 R 数据框中找到零的逐行频率?
r programmingserver side programmingprogramming更新于 2025/4/12 16:22:17
在数据分析中,我们需要非常谨慎地对待重复值,因为它们可能是故意输入的,以在数据中产生偏差,并且该值也可能是零。这种情况发生在我们有缺失数据并且数据收集器用零替换缺失值的情况下,这是一种错误的做法。要找到 R 数据框中零的逐行频率,我们可以使用 rowSums 函数来获取零值,方法是使用语法 −
rowSums(“data_frame_name”==0)
考虑下面的数据框 −
示例
set.seed(189) x1<-sample(0:1,20,replace=TRUE) x2<-sample(0:5,20,replace=TRUE) x3<-sample(0:2,20,replace=TRUE) x4<-sample(0:1,20,replace=TRUE) x5<-sample(0:5,20,replace=TRUE) x6<-sample(0:10,20,replace=TRUE) df1<-data.frame(x1,x2,x3,x4,x5,x6) df1
输出
x1 x2 x3 x4 x5 x6 1 1 5 2 1 3 1 2 1 3 0 0 5 2 3 1 2 2 1 2 5 4 1 4 0 0 0 7 5 0 0 2 0 5 0 6 0 5 1 1 2 5 7 0 5 1 1 1 6 8 0 4 1 0 3 1 9 0 4 1 1 3 4 10 0 1 2 0 4 5 11 1 1 2 0 4 0 12 1 2 0 0 5 1 13 1 3 0 0 1 5 14 1 0 0 0 5 1 15 0 3 0 1 5 10 16 0 3 2 1 3 2 17 1 3 0 1 1 7 18 0 3 1 1 2 9 19 1 3 2 0 3 5 20 1 4 0 1 2 1
计算每行中零的数量 −
示例
df1$Zeros<-rowSums(df1==0) df1
输出
x1 x2 x3 x4 x5 x6 Zeros 1 0 1 1 0 4 3 2 2 0 4 2 1 4 10 1 3 0 3 2 0 0 3 3 4 0 5 0 0 4 4 3 5 1 1 1 0 3 6 1 6 1 2 1 1 4 5 0 7 1 0 2 0 0 4 3 8 1 0 0 1 3 2 2 9 0 2 0 0 5 1 3 10 0 4 1 1 4 2 1 11 0 3 0 0 1 3 3 12 0 0 0 0 1 5 4 13 1 2 0 1 5 8 1 14 1 1 2 1 1 10 0 15 1 3 0 1 4 6 1 16 1 5 2 0 0 8 2 17 0 1 2 0 0 4 3 18 0 5 2 1 3 1 1 19 1 2 2 0 0 2 2 20 1 3 2 1 3 4 0
我们来看另一个例子 −
示例
y1<-sample(0:2,20,replace=TRUE) y2<-sample(0:1,20,replace=TRUE) y3<-sample(0:5,20,replace=TRUE) y4<-sample(0:4,20,replace=TRUE) y5<-sample(0:1,20,replace=TRUE) y6<-sample(0:5,20,replace=TRUE) y7<-sample(0:10,20,replace=TRUE) df2<-data.frame(y1,y2,y3,y4,y5,y6) df2
输出
y1 y2 y3 y4 y5 y6 1 2 1 1 1 0 1 2 0 1 3 1 0 3 3 0 1 5 0 1 1 4 2 1 5 2 1 4 5 0 0 5 0 0 0 6 0 0 1 0 1 4 7 1 0 0 4 1 2 8 0 1 3 1 0 1 9 1 0 4 2 0 1 10 0 0 2 3 0 5 11 2 1 3 2 0 5 12 1 1 5 4 0 2 13 2 0 2 1 0 2 14 2 0 2 4 1 0 15 0 1 3 3 0 1 16 0 1 4 0 1 0 17 1 0 5 2 1 4 18 1 1 3 1 1 0 19 1 1 4 4 1 2 20 2 0 1 1 0 3
计算每行中零的数量 −
示例
df2$No_of_Zeros <-rowSums(df2==0) df2
输出
y1 y2 y3 y4 y5 y6 No_of_Zeros 1 1 0 2 0 1 5 2 2 1 1 0 3 1 2 1 3 1 1 3 4 1 5 0 4 0 1 0 1 0 0 4 5 1 0 2 1 1 2 1 6 1 1 0 4 1 0 2 7 2 1 0 2 0 3 2 8 1 1 3 2 0 1 1 9 2 0 2 0 0 4 3 10 2 1 1 2 0 5 1 11 0 0 1 4 1 3 2 12 2 1 4 3 0 1 1 13 1 1 3 3 0 1 1 14 1 0 3 2 0 0 3 15 0 1 1 0 0 2 3 16 1 0 5 0 1 5 2 17 0 1 0 3 1 5 2 18 2 1 0 3 1 0 2 19 2 0 0 0 0 5 4 20 1 1 5 4 1 4 0