[R] get the sorted index of elements within a column
Dennis Murphy
djmuser at gmail.com
Wed Oct 12 04:55:58 CEST 2011
> m <- matrix(rpois(16, 10), ncol = 4)
> m
[,1] [,2] [,3] [,4]
[1,] 9 12 8 11
[2,] 12 7 11 8
[3,] 12 7 8 8
[4,] 11 11 4 8
> apply(m, 2, sort)
[,1] [,2] [,3] [,4]
[1,] 9 7 4 8
[2,] 11 7 8 8
[3,] 12 11 8 8
[4,] 12 12 11 11
> apply(m, 2, order)
[,1] [,2] [,3] [,4]
[1,] 1 2 4 2
[2,] 4 3 1 3
[3,] 2 4 3 4
[4,] 3 1 2 1
HTH,
Dennis
On Tue, Oct 11, 2011 at 7:05 PM, Wendy <wendy2.qiao at gmail.com> wrote:
> Hi all,
>
> I have matrix, A. I want to sort the elements in each column in a ascending
> order and output the index number their position. For example
>
> A =
>
> [,1] [,2] [,3] [,4]
> [1,] 135.87060 97.55820 121.07722 171.625495
> [2,] 70.19811 36.64529 50.13501 6.319125
> [3,] 26.86293 24.22025 43.42826 26.952587
> [4,] 1.88864 54.97618 12.50913 103.958836
>
> I want the output, B, to be
>
> B =
>
> [,1] [,2] [,3] [,4]
> [1,] 4 4 4 4
> [2,] 3 2 3 1
> [3,] 2 1 2 2
> [4,] 1 3 1 3
>
> I tried "apply(test,1,order)", but it did not work. Could anybody please
> help? Thank you very much.
>
> Wendy
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/get-the-sorted-index-of-elements-within-a-column-tp3896606p3896606.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list