[R] Help with arrays
Marc Schwartz
marc_schwartz at me.com
Thu May 29 18:22:47 CEST 2014
On May 29, 2014, at 11:02 AM, Olivier Charansonney <olivier.charansonney at orange.fr> wrote:
> Hello,
>
> I would like to extract the value in row 1 corresponding to the maximum in
> row 2
>
>
>
> Array W
>
> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [,8] [,9] [,10]
>
> [1,] 651.00000 651.00000 651.00000 651.00000 651.00000 651.00000 651.00000
> 119.00000 78.00000 78.00000
>
> [2,] 13.24184 13.24184 13.24184 13.24184 13.24184 13.24184 13.24184
> 16.19418 15.47089 15.47089
>
>> valinit<-max(W[2,])
>
>> valinit
>
> [1] 16.19418
>
> How to obtain ‘119’
>
> Thanks,
Hi,
Using ?dput can help make it easier for others to recreate your object to test code:
> dput(W)
structure(c(651, 13.24184, 651, 13.24184, 651, 13.24184, 651,
13.24184, 651, 13.24184, 651, 13.24184, 651, 13.24184, 119, 16.19418,
78, 15.47089, 78, 15.47089), .Dim = c(2L, 10L))
W <- structure(c(651, 13.24184, 651, 13.24184, 651, 13.24184, 651,
13.24184, 651, 13.24184, 651, 13.24184, 651, 13.24184, 119, 16.19418,
78, 15.47089, 78, 15.47089), .Dim = c(2L, 10L))
See ?which.max, which returns the index of the *first* maximum in the vector passed to it:
> W[1, which.max(W[2, ])]
[1] 119
You should consider what happens if there is more than one of the maximum value in the first row and if it might correspond to non-unique values in the second row.
Regards,
Marc Schwartz
More information about the R-help
mailing list