📌  相关文章
📜  如何在 R 中提取具有最小值或最大值的数据帧行?

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

如何在 R 中提取具有最小值或最大值的数据帧行?

R编程语言中行和列的表格排列形成数据框支持多种访问和修改数据的方式。可以轻松地对数据框单元格值应用查询和聚合函数,如 min、max 和 count。因此,基于单元格中包含的值访问数据帧的子集相对非常容易。

示例 1:根据整个数据框值确定具有最小值或最大值的行。

通过对数据帧的每一行和每一列分别使用两个循环,对数据帧单元格进行迭代。单元格值分别与初始最小值和最大值进行比较,并在值满足约束的情况下进行更新。此外,声明了一个变量以保持当前行索引满足条件。然后,访问数据帧的该索引处的行。时间复杂度是关于数据帧大小的多项式。

R
# declaring a data frame in R
data_frame = data.frame(C1 = c(5:8),
                        C2 = c(1:4),
                        C3 = c(9:12),
                        C4 = c(13:16))
  
print("Original data frame")
print(data_frame)
  
# declaring initial values for min and max
min = 32767
max = -32767
min_row_indx = 0
max_row_indx = 0
  
# looping over the data frame values
for (i in 1:nrow(data_frame)){
    
# for-loop over columns
for(j in 1:ncol(data_frame)) {   
    
        # checking if the dataframe
          # cell value is less than minimum
        if(data_frame[i,j]max){
            
              # replacing the minimum
            # with the smaller value
            max = data_frame[i,j]
            
            # updating the row with the
              # smallest value found uptil now
            max_row_indx = i
        }
    }
}
# printing the row with minimum value
print ("Row with minimum value in the data frame")
print (data_frame[min_row_indx,])
  
# printing the row with maximum value
print ("Row with maximum value in the data frame")
print (data_frame[max_row_indx,])


R
# declaring a data frame in R
data_frame = data.frame(C1 = c(1:4),
                        C2 = c( 5:8),
                        C3 = c(9:12),
                        C4 = c(13:16))
  
print("Original data frame")
print(data_frame)
  
# extracting the row with
# maximum value in C2 column
print ("Row with max C2 value")
data_frame[which.max(data_frame$C2),]
  
# extracting the row with 
# minimum value in C4 column
print ("Row with min C4 value")
data_frame[which.min(data_frame$C4),]


R
# declaring a data frame in R
data_frame = data.frame(C1= c("a","b","c","d"),
                        C2= c("geeks","dataframe","in","R"),
                        C3= c(9:12),C4=c(13:16))
  
print("Original data frame")
print(data_frame)
  
# extracting the row with maximum value in 
# C2 column
print ("Row with max C1 value")
data_frame[which.max(data_frame$C1),]
  
# extracting the row with minimum value in 
# C4 column
print ("Row with min C2 value")
data_frame[which.min(data_frame$C2),]


输出:



[1] "Original data frame"
 C1 C2 C3 C4
1  5  1  9 13
2  6  2 10 14
3  7  3 11 15
4  8  4 12 16
[1] "Row with minimum value in the data frame"
 C1 C2 C3 C4
1  5  1  9 13
[1] "Row with maximum value in the data frame"
 C1 C2 C3 C4
4  8  4 12 16

示例 2:根据数据框列确定具有最小值或最大值的行

R 中的函数which.min() 可用于计算指定为参数的对象中所有值的最小值,无论它是列表、矩阵还是数据框。类似地, which.max() 计算所有值中的最大值。为了选择数据框的特定列,我们使用df$colname ,然后将其用作数据框的索引,以使用指定的聚合函数提取完整行。这种方法可以应用于所有数据类型,数字、字符串以及因子。所需的时间复杂度与列长度呈线性关系,因为我们直接访问并比较该特定列的所有值。

R 中的以下语法用于提取参数中指定的列中具有最小值或最大值的行:

代码:

电阻

# declaring a data frame in R
data_frame = data.frame(C1 = c(1:4),
                        C2 = c( 5:8),
                        C3 = c(9:12),
                        C4 = c(13:16))
  
print("Original data frame")
print(data_frame)
  
# extracting the row with
# maximum value in C2 column
print ("Row with max C2 value")
data_frame[which.max(data_frame$C2),]
  
# extracting the row with 
# minimum value in C4 column
print ("Row with min C4 value")
data_frame[which.min(data_frame$C4),]

输出:

[1] "Original data frame"
 C1 C2 C3 C4
1  1  5  9 13
2  2  6 10 14
3  3  7 11 15
4  4  8 12 16
[1] "Row with max C2 value"
 C1 C2 C3 C4
4  4  8 12 16
[1] "Row with min C4 value"
 C1 C2 C3 C4
1  1  5  9 13

如果数据框包含字符串类型变量值,则在按字典顺序对该数据进行排序时计算最小值和最大值。

电阻

# declaring a data frame in R
data_frame = data.frame(C1= c("a","b","c","d"),
                        C2= c("geeks","dataframe","in","R"),
                        C3= c(9:12),C4=c(13:16))
  
print("Original data frame")
print(data_frame)
  
# extracting the row with maximum value in 
# C2 column
print ("Row with max C1 value")
data_frame[which.max(data_frame$C1),]
  
# extracting the row with minimum value in 
# C4 column
print ("Row with min C2 value")
data_frame[which.min(data_frame$C2),]

输出

[1] "Original data frame"
 C1        C2 C3 C4
1  a     geeks  9 13
2  b dataframe 10 14
3  c        in 11 15
4  d         R 12 16
[1] "Row with max C1 value"
 C1 C2 C3 C4
4  d  R 12 16
[1] "Row with min C2 value"
 C1        C2 C3 C4
2  b dataframe 10 14