📌  相关文章
📜  如何在R数据框中找到行值的比例?

📅  最后修改于: 2022-05-13 01:54:47.253000             🧑  作者: Mango

如何在R数据框中找到行值的比例?

数据框中行值的比例等于单元格值除以属于该整行的单元格值的总和。一个数据框中所有行比例值的总和等于1。在这篇文章中,我们将看到如何在R编程语言中找到一个数据框中的行值比例。

示例 1:使用两个 for 循环对矩阵进行迭代。我们在执行行迭代时计算每一行的行总和,然后将单元格值除以行总和。该值被重新分配给数据框原始单元格值。执行此操作所需的时间复杂度等效于 O(n * m),其中 n 是编号。行数,m 是数据框中的列数。

以下代码片段说明了这种方法的应用:

R
# declaring a data frame in R
data_frame = data.frame(C1= c(0,1,2,3),
                        C2 = c(1:4),
                        C3 = c(9:12))
  
print("Original data frame")
print(data_frame)
  
# looping over the rows of data frame
for (i in 1:nrow(data_frame)){
    
    # looping over the columns of data frame
    for (j in 1:ncol(data_frame)){
        
        # computing sum of row i 
        row_sum <- sum(data_frame[i,])
        
        # calculating row proportion of the cell 
        # value
        data_frame[i,j] <- data_frame[i,j]/row_sum
    }
}
  
# printing modified data frame
print ("Modified data frame")
print (data_frame)


R
# declaring a data frame in R
data_frame = data.frame(C1= c(0,1,2,3),
                        C2 = c(2,3,2,3),
                        C3 = c(9:12))
  
print("Original data frame")
print(data_frame)
  
# divides each cell value with corresponding
# row sum value
data_frame<-data_frame/rowSums(data_frame)
  
# printing modified data frame
print ("Modified data frame")
print (data_frame)


R
# declaring a data frame in R
data_frame = data.frame(C1= c(1+2i,3i,6+5i,1+2i),
                        C2 = c(2,3,2,3),
                        C3 = c(9:12))
  
print("Original data frame")
print(data_frame)
  
# divides each cell value with corresponding row sum value
data_frame<-data_frame/rowSums(data_frame)
  
# printing modified data frame
print ("Modified data frame")
print (data_frame)


输出:



[1] "Original data frame"
 C1 C2 C3
1  0  1  9
2  1  2 10
3  2  3 11
4  3  4 12
[1] "Modified data frame"
         C1        C2        C3
1 0.00000000 0.1000000 0.9890110
2 0.07692308 0.1656051 0.9763215
3 0.12500000 0.2123894 0.9702410
4 0.15789474 0.2475570 0.9673166

示例 2:使用rowSums()方法

此方法循环遍历数据帧并迭代计算数据帧中每一行的总和。对于这种方法的应用,输入数据框本质上必须是数字。但是,这种方法也适用于复数。 R 中的以下语法可用于计算单元格值的行比例,其中输出必须显式存储到新数据框中:

代码:

电阻

# declaring a data frame in R
data_frame = data.frame(C1= c(0,1,2,3),
                        C2 = c(2,3,2,3),
                        C3 = c(9:12))
  
print("Original data frame")
print(data_frame)
  
# divides each cell value with corresponding
# row sum value
data_frame<-data_frame/rowSums(data_frame)
  
# printing modified data frame
print ("Modified data frame")
print (data_frame)

输出:

[1] "Original data frame"
 C1 C2 C3
1  0  2  9
2  1  3 10
3  2  2 11
4  3  3 12
[1] "Modified data frame"
         C1        C2        C3
1 0.00000000 0.1818182 0.8181818
2 0.07142857 0.2142857 0.7142857
3 0.13333333 0.1333333 0.7333333
4 0.16666667 0.1666667 0.6666667

以下代码片段说明了复数数据框上行比例的计算:

电阻

# declaring a data frame in R
data_frame = data.frame(C1= c(1+2i,3i,6+5i,1+2i),
                        C2 = c(2,3,2,3),
                        C3 = c(9:12))
  
print("Original data frame")
print(data_frame)
  
# divides each cell value with corresponding row sum value
data_frame<-data_frame/rowSums(data_frame)
  
# printing modified data frame
print ("Modified data frame")
print (data_frame)

输出

[1] "Original data frame"
   C1 C2 C3
1 1+2i  2  9
2 0+3i  3 10
3 6+5i  2 11
4 1+2i  3 12
[1] "Modified data frame"
                   C1                    C2                   C3
1 0.1081081+0.1486486i 0.1621622-0.02702703i 0.7297297-0.1216216i
2 0.0505618+0.2191011i 0.2191011-0.05056180i 0.7303371-0.1685393i
3 0.3601036+0.1683938i 0.0984456-0.02590674i 0.5414508-0.1424870i
4 0.0769231+0.1153846i 0.1846154-0.02307692i 0.7384615-0.0923077i

所有值都以整数+0i 的形式计算,并返回相应的行比例值。