Reputation: 6409
Let's say I have an array of the following form:
,,1
[,1] [,2] [,3]
[1,] 34 3 9
[2,] 45 5 8
.
.
,,2
[,1] [,2] [,3]
[1,] 21 5 10
[2,] 94 2 10
.
.
First I want to select the highest value in the third column of each matrix:
apply(array[,3,],2,max)
This returns a vector with the highest element in each matrix.
Now I would like to select those values in the second column [,2] of each matrix which have the max value in the third column [,3].
How can I do this?
From the example above the desired output would be:
,,1
3
,,2
5,2
Upvotes: 0
Views: 162
Reputation: 887481
You may try
lapply(1:dim(arr1)[3], function(i) {
x1 <- arr1[,,i]
x1[,2][x1[,3]==max(x1[,3])]})
#[[1]]
#[1] 3
#[[2]]
#[1] 5 2
Upvotes: 1