How to find the proportion of row values in R dataframe?
Last Updated :
07 Apr, 2021
The proportion of row value in a data frame is equivalent to the cell value divided by the summation of the cell values belonging to that entire row. The sum of all the row proportion values in a data frame is equivalent to 1. In this article, we are going to see how to find the proportion of row values in a data frame in R Programming Language.
Example 1: An iteration over the matrix is carried out, using two for loops. We compute the row sum for each row while performing row iteration, and then divide the cell value by the row sum. This value is reassigned to the data frame original cell value. The time complexity required to perform this is equivalent to O(n * m), where n is the no. of rows and m is the number of columns in the data frame.
The following code snippet illustrates the application of this approach :
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)
for (i in 1: nrow (data_frame)){
for (j in 1: ncol (data_frame)){
row_sum <- sum (data_frame[i,])
data_frame[i,j] <- data_frame[i,j]/row_sum
}
}
print ( "Modified data frame" )
print (data_frame)
|
Output:
[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
Example 2 : Using rowSums() method
This method loops over the data frame and iteratively computes the sum of each row in the data frame. For the application of this method, the input data frame must be numeric in nature. However, this method is also applicable for complex numbers. The following syntax in R can be used to compute the row proportion of cell values, wherein the output has to be explicitly stored into a new data frame :
Syntax: mdf<-df/rowSums(df)
Arguments : df – The data frame to compute the proportion of row values
Code:
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)
data_frame<-data_frame/ rowSums (data_frame)
print ( "Modified data frame" )
print (data_frame)
|
Output:
[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
The following code snippet illustrates the calculation of row proportion on the complex numbers’ data frame :
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)
data_frame<-data_frame/ rowSums (data_frame)
print ( "Modified data frame" )
print (data_frame)
|
Output
[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
All the values are evaluated in the form of a whole number + 0i and the corresponding row proportion value is returned.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...