获取列名称,该列名称在矩阵的行内保存最大值,该矩阵在数组中保持单独的最大值

时间:2022-09-27 20:17:14

For instance given:

例如给出:

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))

I want to reference within this array, the matrix that has the max dim3 value at the (3rd row, 4th column) location.

我想在这个数组中引用在(第3行,第4列)位置具有max dim3值的矩阵。

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.

在识别出该矩阵后,我想返回在矩阵(第3行,第1列)到(第3行,第3列)范围内具有最大值的列名。

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".

所以我希望发生的是Jerry3被引用,因为数字47存储在第3行,第4列,然后在Jerry3中,我希望第3行中的最大数字被引用,这将是43,并且最终,我需要返回的(我需要的唯一值)是列名称,它将是“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".

这就是我需要知道如何做,获得“R”并将其分配给变量,即“column_ref”,使得column_ref < - “R”。

Please Please Please help.

请请帮忙。

2 个解决方案

#1


14  

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").

...然后names返回索引的名称(“R”)。

#2


0  

This post helped me to solve a data.frame general problem.
I have repeated measures for groups, G1 e G2.

这篇文章帮我解决了data.frame的一般问题。我已经为团体重复测量了G1 e G2。

> str(df)
'data.frame':   6 obs. of  15 variables:
$ G1       : num  0 0 2 2 8 8
$ G2       : logi  FALSE TRUE FALSE TRUE FALSE TRUE
$ e.10.100 : num  26.41 -11.71 27.78 3.17 26.07 ...
$ e.10.250 : num  27.27 -12.79 29.16 3.19 26.91 ...
$ e.20.100 : num  29.96 -12.19 26.19 3.44 27.32 ...
$ e.20.100d: num  26.42 -13.16 28.26 4.18 25.43 ...
$ e.20.200 : num  24.244 -18.364 29.047 0.553 25.851 ...
$ e.20.50  : num  26.55 -13.28 29.65 4.34 27.26 ...
$ e.20.500 : num  27.94 -13.92 27.59 2.47 25.54 ...
$ e.20.500d: num  24.4 -15.63 26.78 4.86 25.39 ...
$ e.30.100d: num  26.543 -15.698 31.849 0.572 29.484 ...
$ e.30.250 : num  26.776 -16.532 28.961 0.813 25.407 ...
$ e.50.100 : num  25.995 -14.249 28.697 0.803 27.852 ...
$ e.50.100d: num  26.1 -12.7 27.1 2.5 27.4 ...
$ e.50.500 : num  28.78 -9.39 25.77 2.73 23.73 ..

I need to know which measure (column) has the best (max) result. And I need to disconsider grouping columns.
I ended up with this function

我需要知道哪个度量(列)具有最佳(最大)结果。我需要考虑分组列。我最终得到了这个功能

apply(df[colIni:colFim], 1, function(x) colnames(df)[which.max(x)+(colIni-1)] 
#colIni: first column to consider; colFim: last column to consider

After having column name, another tiny function to get the max value

拥有列名后,另一个微小的函数来获取最大值

apply(dfm,1,function(x) x[x[1]])

And the function to solve similar problems, that return the column and the max value

并且该函数解决了类似的问题,即返回列和最大值

mxCol=function(df, colIni, colFim){ #201609
  if(missing(colIni)) colIni=1
  if(missing(colFim)) colFim=ncol(df)
  if(colIni>=colFim) { print('colIni>=ColFim'); return(NULL)}
  dfm=cbind(mxCol=apply(df[colIni:colFim], 1, function(x) colnames(df)[which.max(x)+(colIni-1)])
           ,df)
  dfm=cbind(mxVal=as.numeric(apply(dfm,1,function(x) x[x[1]]))
           ,dfm)
  return(dfm)
}

In this case,

在这种情况下,

> mxCol(df,3)[1:11]
   mxVal     mxCol G1    G2 e.10.100 e.10.250 e.20.100 e.20.100d e.20.200 e.20.50 e.20.500
1 29.958  e.20.100  0 FALSE   26.408   27.268   29.958    26.418   24.244  26.553   27.942
2 -9.395  e.50.500  0  TRUE  -11.708  -12.789  -12.189   -13.162  -18.364 -13.284  -13.923
3 31.849 e.30.100d  2 FALSE   27.782   29.158   26.190    28.257   29.047  29.650   27.586
4  4.862 e.20.500d  2  TRUE    3.175    3.190    3.439     4.182    0.553   4.337    2.467
5 29.484 e.30.100d  8 FALSE   26.069   26.909   27.319    25.430   25.851  27.262   25.535
6 -9.962  e.30.250  8  TRUE  -11.362  -12.432  -15.960   -11.760  -12.832 -12.771  -12.810

#1


14  

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").

...然后names返回索引的名称(“R”)。

#2


0  

This post helped me to solve a data.frame general problem.
I have repeated measures for groups, G1 e G2.

这篇文章帮我解决了data.frame的一般问题。我已经为团体重复测量了G1 e G2。

> str(df)
'data.frame':   6 obs. of  15 variables:
$ G1       : num  0 0 2 2 8 8
$ G2       : logi  FALSE TRUE FALSE TRUE FALSE TRUE
$ e.10.100 : num  26.41 -11.71 27.78 3.17 26.07 ...
$ e.10.250 : num  27.27 -12.79 29.16 3.19 26.91 ...
$ e.20.100 : num  29.96 -12.19 26.19 3.44 27.32 ...
$ e.20.100d: num  26.42 -13.16 28.26 4.18 25.43 ...
$ e.20.200 : num  24.244 -18.364 29.047 0.553 25.851 ...
$ e.20.50  : num  26.55 -13.28 29.65 4.34 27.26 ...
$ e.20.500 : num  27.94 -13.92 27.59 2.47 25.54 ...
$ e.20.500d: num  24.4 -15.63 26.78 4.86 25.39 ...
$ e.30.100d: num  26.543 -15.698 31.849 0.572 29.484 ...
$ e.30.250 : num  26.776 -16.532 28.961 0.813 25.407 ...
$ e.50.100 : num  25.995 -14.249 28.697 0.803 27.852 ...
$ e.50.100d: num  26.1 -12.7 27.1 2.5 27.4 ...
$ e.50.500 : num  28.78 -9.39 25.77 2.73 23.73 ..

I need to know which measure (column) has the best (max) result. And I need to disconsider grouping columns.
I ended up with this function

我需要知道哪个度量(列)具有最佳(最大)结果。我需要考虑分组列。我最终得到了这个功能

apply(df[colIni:colFim], 1, function(x) colnames(df)[which.max(x)+(colIni-1)] 
#colIni: first column to consider; colFim: last column to consider

After having column name, another tiny function to get the max value

拥有列名后,另一个微小的函数来获取最大值

apply(dfm,1,function(x) x[x[1]])

And the function to solve similar problems, that return the column and the max value

并且该函数解决了类似的问题,即返回列和最大值

mxCol=function(df, colIni, colFim){ #201609
  if(missing(colIni)) colIni=1
  if(missing(colFim)) colFim=ncol(df)
  if(colIni>=colFim) { print('colIni>=ColFim'); return(NULL)}
  dfm=cbind(mxCol=apply(df[colIni:colFim], 1, function(x) colnames(df)[which.max(x)+(colIni-1)])
           ,df)
  dfm=cbind(mxVal=as.numeric(apply(dfm,1,function(x) x[x[1]]))
           ,dfm)
  return(dfm)
}

In this case,

在这种情况下,

> mxCol(df,3)[1:11]
   mxVal     mxCol G1    G2 e.10.100 e.10.250 e.20.100 e.20.100d e.20.200 e.20.50 e.20.500
1 29.958  e.20.100  0 FALSE   26.408   27.268   29.958    26.418   24.244  26.553   27.942
2 -9.395  e.50.500  0  TRUE  -11.708  -12.789  -12.189   -13.162  -18.364 -13.284  -13.923
3 31.849 e.30.100d  2 FALSE   27.782   29.158   26.190    28.257   29.047  29.650   27.586
4  4.862 e.20.500d  2  TRUE    3.175    3.190    3.439     4.182    0.553   4.337    2.467
5 29.484 e.30.100d  8 FALSE   26.069   26.909   27.319    25.430   25.851  27.262   25.535
6 -9.962  e.30.250  8  TRUE  -11.362  -12.432  -15.960   -11.760  -12.832 -12.771  -12.810