📌  相关文章
📜  在 R DataFrame 中查找最大值的索引

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

在 R DataFrame 中查找最大值的索引

在本文中,我们将看到如何在 R 编程语言中从 DataFrame 中找到最大值的索引

我们可以使用which.max()函数找到数据帧中的最大值索引。

句法:

“$”用于访问数据帧的特定列。



下面给出了各种实现,描述了各种数据类型和情况,以帮助您更好地理解。

示例 1:

R
# vector 1
data1=c("sravan","bobby","pinkey","rohith","gnanesh")
  
# vector 2
data2=c(98,78,79,97,89)
  
# creating a dataframe with names and marks 
# using above vectors
final <- data.frame(names=data1,marks=data2)
                     
print(final)
  
# display the maximum value index in 2 nd column
# (marks column) in a dataframe
print(paste("highest index is : ",which.max(final$marks)))


R
# vector 1
data1=c("sravan","bobby","pinkey","rohith",
        "gnanesh",'divya',"satwik","chandu")
  
# vector 2
data2=c(98,78,79,97,89,89,99,99)
  
# creating a dataframe with names and marks 
# using above vectors
final <- data.frame(names=data1,marks=data2)
                     
print(final)
  
print(paste("highest index is : ",which.max(final$marks)))


R
# vector 1
data1=c("sravan","bobby","pinkey","rohith",
        "gnanesh",'divya',"satwik","zhandu")
  
# vector 2
data2=c(98,78,79,97,89,89,99,99)
  
# creating a dataframe with names and marks 
# using above vectors
final <- data.frame(names=data1,marks=data2)
                     
print(final)
  
# display maximum value index for character values
print(paste("highest index is : ",which.max(final$names)))


R
# vector 1 that contains NA values as characters
data1=c(NA,"sravan",NA,NA,NA)
  
# vector 2 comtains all data
data2=c(102,98,98,102,102)
  
# creating a dataframe with names and marks
# using above vectors
final <- data.frame(names=data1,marks=data2)
                     
print(final)
  
# display maximum value index for character values
print(paste("highest index is : ",which.max(final$names)))
  
# display maximum value index for  marks values
print(paste("highest index is : ",which.max(final$marks)))


R
# vector contains all same data
data2=c(102,102,102,102,102)
  
# creating a dataframe marks using above vector
final <- data.frame(marks=data2)
                     
print(final)
  
# display maximum value index for  marks values
print(paste("highest index is : ",which.max(final$marks)))


R
# vector contains all NA's
data2=c(NA,NA)
  
# creating a dataframe marks using
# above vector
final <- data.frame(marks=data2)
                     
print(final)
  
# display maximum value index for  marks values
print(paste("highest index is : ",which.max(final$marks)))


输出:

如果有多个最大值,则它将返回重复的第一个数字的索引。

示例 2:

电阻



# vector 1
data1=c("sravan","bobby","pinkey","rohith",
        "gnanesh",'divya',"satwik","chandu")
  
# vector 2
data2=c(98,78,79,97,89,89,99,99)
  
# creating a dataframe with names and marks 
# using above vectors
final <- data.frame(names=data1,marks=data2)
                     
print(final)
  
print(paste("highest index is : ",which.max(final$marks)))

输出:

如果数据是字符类型,它将使用 ASCII 值找到最大值。

示例 3:

电阻

# vector 1
data1=c("sravan","bobby","pinkey","rohith",
        "gnanesh",'divya',"satwik","zhandu")
  
# vector 2
data2=c(98,78,79,97,89,89,99,99)
  
# creating a dataframe with names and marks 
# using above vectors
final <- data.frame(names=data1,marks=data2)
                     
print(final)
  
# display maximum value index for character values
print(paste("highest index is : ",which.max(final$names)))

输出:

示例 4:

电阻

# vector 1 that contains NA values as characters
data1=c(NA,"sravan",NA,NA,NA)
  
# vector 2 comtains all data
data2=c(102,98,98,102,102)
  
# creating a dataframe with names and marks
# using above vectors
final <- data.frame(names=data1,marks=data2)
                     
print(final)
  
# display maximum value index for character values
print(paste("highest index is : ",which.max(final$names)))
  
# display maximum value index for  marks values
print(paste("highest index is : ",which.max(final$marks)))

输出:



如果包含所有值的行都相同,则所有值都更高。所以它将返回第一个元素的索引。

示例 5:

电阻

# vector contains all same data
data2=c(102,102,102,102,102)
  
# creating a dataframe marks using above vector
final <- data.frame(marks=data2)
                     
print(final)
  
# display maximum value index for  marks values
print(paste("highest index is : ",which.max(final$marks)))

输出:

如果数据包含 NA 值,则它将返回空值。

示例 6:

电阻

# vector contains all NA's
data2=c(NA,NA)
  
# creating a dataframe marks using
# above vector
final <- data.frame(marks=data2)
                     
print(final)
  
# display maximum value index for  marks values
print(paste("highest index is : ",which.max(final$marks)))

输出: