Jon*_*ton 10 indexing r max which
例如给出:
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))
Run Code Online (Sandbox Code Playgroud)
我想在这个数组中引用在(第3行,第4列)位置具有max dim3值的矩阵.
在识别该矩阵后,我想返回在矩阵(第3行,第1列)到(第3行,第3列)范围内具有最大值的列名.
所以我希望发生的是Jerry3被引用,因为数字47存储在第3行,第4列,然后在Jerry3中,我希望第3行中的最大数字被引用,这将是43,并且最终,我需要返回的(我需要的唯一值)是列名称,即"R".
这就是我需要知道如何做的,获得得到"R"并将其分配给变量,即"column_ref",使得column_ref < - "R".
请请帮忙.
Tom*_*mmy 14
这应该这样做 - 如果我理解正确的话:
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"
Run Code Online (Sandbox Code Playgroud)
一些解释:
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)
Run Code Online (Sandbox Code Playgroud)
...然后names返回索引的名称("R").