获取列名称,该列名称在矩阵的行内保存最大值,该矩阵在数组中保持单独的最大值 [英] Getting column name which holds a max value within a row of a matrix holding a separate max value within an array
问题描述
例如给出:
dim1 <- c("P","PO","C","T")
dim2 <- c("LL","RR","R","Y")
dim3 <- c("Jerry1", "Jerry2", "Jerry3")
Q <- array(1:48, c(4, 4, 3), dimnames = list(dim1, dim2, dim3))
我想在这个数组中引用,在(第3行,第4列)位置具有max dim3值的矩阵。
I want to reference within this array, the matrix that has the max dim3 value at the (3rd row, 4th column) location.
识别出矩阵后,我想将矩阵(第3行,第1列)中具有最大值的列名返回到(第3行,第3列)范围。
Upon identifying that matrix, I want to return the column name which has the maximum value within the matrix's (3rd Row, 1st Column) to (3rd Row, 3rd Column) range.
所以我希望发生的是Jerry3被引用,因为数字47存储在第3行,第4列,然后在Jerry3中,我希望第3行中的最大数量为得到引用的是43,最终,我需要返回的(我需要的唯一值)就是列名称R。
So what I'd hope to happen is that Jerry3 gets referenced because the number 47 is stored in its 3rd row, 4th column, and then within Jerry3, I would want the maximum number in row 3 to get referenced which would be 43, and ultimately, what I need returned (the only value I need) is then the column name which would be "R".
这就是什么我需要知道怎么做,获得那个R和将它分配给一个变量,即column_ref,这样column_ref< - R。
That's what I need to know how to do, obtain get that "R" and assign it to a variable, i.e. "column_ref", such that column_ref <- "R".
请请帮助。
推荐答案
这应该这样做 - 如果我理解正确的话:
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"
一些解释:
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)
...然后 names
返回索引的名称(R) 。
...and then names
returns the name of the index ("R").
这篇关于获取列名称,该列名称在矩阵的行内保存最大值,该矩阵在数组中保持单独的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!