简体   繁体   English

在R中找到矩阵的最大元素

[英]Finding Largest Element of a Matrix in R

I am using R for the first time and need to write a function that takes in a matrix with 2 columns. 我第一次使用R,需要编写一个包含2列矩阵的函数。 For example 例如

col1 col2
  2   0.2
  3   0.2
  4   0.3
  5   0.1
  6   0.2

Based on which number is highest in col2 (which is 0.3), I need to return the corresponding number in col1 (which is 4). 基于col2中哪个数字最高(它是0.3),我需要在col1中返回相应的数字(它是4)。 How can I go about doing this in R? 我该如何在R中执行此操作?

We could also order the dataset by the second column and get the first element 我们还可以按第二列对数据集进行order并获取第一个元素

df1[order(-df1[,'col2']),1][1]
#[1] 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM