Getting column name which holds a max value within a row of a matrix holding a separate max value within an array

后端 未结 3 1923
遥遥无期
遥遥无期 2021-02-13 19:04

For instance given:

dim1 <- c("P","PO","C","T")
dim2 <- c("LL","RR","R","Y")         


        
3条回答
  •  遥遥无期
    2021-02-13 19:46

    This should do it - if I understand correctly:

    Q <- array(1:48, c(4,4,3), dimnames=list(
      c("P","PO","C","T"), c("LL","RR","R","Y"), c("Jerry1", "Jerry2", "Jerry3")))
    
    column_ref <- names(which.max(Q[3,1:3, which.max(Q[3,4,])]))[1] # "R"
    

    Some explanation:

    which.max(Q[3,4,]) # return the index of the "Jerry3" slice (3)
    which.max(Q[3,1:3, 3]) # returns the index of the "R" column (3)
    

    ...and then names returns the name of the index ("R").

提交回复
热议问题