如何找到 R 数据框中行值的比例?
r programmingserver side programmingprogramming更新于 2025/4/13 13:22:17
如果我们将每行值除以特定行中所有值的总和,则可以计算出行值的比例。因此,比例的总和将等于 1。这可以通过将数据框除以行总和来实现,为此我们可以使用以下语法 −
语法
data_frame_name/rowSums(data_frame_name)
考虑以下数据框 −
示例
set.seed(111) x1<-rpois(20,2) x2<-rpois(20,5) x3<-round(runif(20,2,5),0) x4<-round(runif(20,2,4),0) df1<-data.frame(x1,x2,x3,x4) df1
输出
x1 x2 x3 x4 1 2 4 4 2 2 3 4 3 3 3 1 4 4 2 4 2 4 5 3 5 1 9 4 4 6 2 4 3 2 7 0 6 3 2 8 2 4 5 3 9 2 7 4 2 10 0 5 4 3 11 2 2 4 4 12 2 5 4 3 13 0 5 2 3 14 0 5 3 2 15 1 4 5 3 16 2 6 4 3 17 1 2 4 3 18 5 7 3 4 19 1 6 2 2 20 2 7 4 4
查找数据框 df1 中每行的比例 −
示例
df1<-df1/rowSums(df1) df1
输出
x1 x2 x3 x4 1 0.16666667 0.3333333 0.3333333 0.1666667 2 0.23076923 0.3076923 0.2307692 0.2307692 3 0.09090909 0.3636364 0.3636364 0.1818182 4 0.14285714 0.2857143 0.3571429 0.2142857 5 0.05555556 0.5000000 0.2222222 0.2222222 6 0.18181818 0.3636364 0.2727273 0.1818182 7 0.00000000 0.5454545 0.2727273 0.1818182 8 0.14285714 0.2857143 0.3571429 0.2142857 9 0.13333333 0.4666667 0.2666667 0.1333333 10 0.00000000 0.4166667 0.3333333 0.2500000 11 0.16666667 0.1666667 0.3333333 0.3333333 12 0.14285714 0.3571429 0.2857143 0.2142857 13 0.00000000 0.5000000 0.2000000 0.3000000 14 0.00000000 0.5000000 0.3000000 0.2000000 15 0.07692308 0.3076923 0.3846154 0.2307692 16 0.13333333 0.4000000 0.2666667 0.2000000 17 0.10000000 0.2000000 0.4000000 0.3000000 18 0.26315789 0.3684211 0.1578947 0.2105263 19 0.09090909 0.5454545 0.1818182 0.1818182 20 0.11764706 0.4117647 0.2352941 0.2352941
我们来看另一个例子 −
示例
y1<-sample(0:5,20,replace=TRUE) y2<-sample(0:9,20,replace=TRUE) y3<-sample(1:10,20,replace=TRUE) y4<-sample(1:50,20) y5<-sample(10:100,20) df2<-data.frame(y1,y2,y3,y4,y5) df2
输出
y1 y2 y3 y4 y5 1 4 5 3 48 87 2 4 6 10 41 76 3 2 5 7 26 36 4 2 1 5 44 82 5 4 8 2 4 80 6 1 1 3 35 12 7 5 5 9 10 84 8 3 3 6 1 93 9 1 3 8 9 15 10 0 4 4 19 83 11 4 5 4 24 65 12 0 7 10 3 49 13 1 5 6 27 64 14 1 5 2 47 10 15 1 6 3 45 56 16 4 0 2 33 28 17 2 9 3 32 96 18 0 3 6 5 52 19 0 7 5 15 61 20 2 6 3 31 98
查找数据框 df2 中每行的比例 −
示例
df2<-df2/rowSums(df2) df2
输出
y1 y2 y3 y4 y5 1 0.027210884 0.034013605 0.02040816 0.326530612 0.5918367 2 0.029197080 0.043795620 0.07299270 0.299270073 0.5547445 3 0.026315789 0.065789474 0.09210526 0.342105263 0.4736842 4 0.014925373 0.007462687 0.03731343 0.328358209 0.6119403 5 0.040816327 0.081632653 0.02040816 0.040816327 0.8163265 6 0.019230769 0.019230769 0.05769231 0.673076923 0.2307692 7 0.044247788 0.044247788 0.07964602 0.088495575 0.7433628 8 0.028301887 0.028301887 0.05660377 0.009433962 0.8773585 9 0.027777778 0.083333333 0.22222222 0.250000000 0.4166667 10 0.000000000 0.036363636 0.03636364 0.172727273 0.7545455 11 0.039215686 0.049019608 0.03921569 0.235294118 0.6372549 12 0.000000000 0.101449275 0.14492754 0.043478261 0.7101449 13 0.009708738 0.048543689 0.05825243 0.262135922 0.6213592 14 0.015384615 0.076923077 0.03076923 0.723076923 0.1538462 15 0.009009009 0.054054054 0.02702703 0.405405405 0.5045045 16 0.059701493 0.000000000 0.02985075 0.492537313 0.4179104 17 0.014084507 0.063380282 0.02112676 0.225352113 0.6760563 18 0.000000000 0.045454545 0.09090909 0.075757576 0.7878788 19 0.000000000 0.079545455 0.05681818 0.170454545 0.6931818 20 0.014285714 0.042857143 0.02142857 0.221428571 0.7000000