【问题标题】:How can I find rows and columns for a specific given value in the matrix?如何在矩阵中找到特定给定值的行和列?
【发布时间】:2020-11-26 14:41:35
【问题描述】:

我有一份关于 NBA 10 名球员的得分和比赛数据。

我想找出从 2005 年到 2014 年篮球场均得分最高的人,并编写了以下代码:

为了找到每个玩家的 PPG,我这样写:

Points
P<-round(Points/Games,2)
P

要找到最高的 PPG,

M<-max(P,na.rm=T)

得到35.4的输出

现在,我只看矩阵就知道是科比。但是如何编写代码来获取该元素的行和列呢?

数据来源:超级数据科学

【问题讨论】:

    标签: r matrix rows


    【解决方案1】:

    欢迎来到 Stack Overflow。下次尝试包含示例数据。它可以帮助我们帮助您。她是使用dplyr 包的解决方案。它是Rtidyverse 方言的一部分。如果您只是在学习 R,请安装 Tidyverse 软件包套件。它会是你最好的朋友。

    data <- data.frame(points = c(10, 12, 12, 4), 
                       games = c(2, 3, 2, 1), 
                       dude = c("a", "b", "c", "d"))
    
    library(dplyr) 
    theDude <- data %>%  #start with the data above and pipe it to the next line
        mutate(pointsPerGame = points/games) %>%   # create the average variable and pipe to next line
        filter(pointsPerGame == max(pointsPerGame)) %>%  # keep only the record(s) with the largest average and pipe to the next line
        select(dude) # keep the person's name
    

    【讨论】:

      【解决方案2】:

      也许你可以试试which 选项arr.ind = TRUE

      which(P == max(P),arr.ind = TRUE)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-24
        • 1970-01-01
        • 2018-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多