[R] Can a matrix with several rows and columns ...

Sarah Goslee sarah.goslee at gmail.com
Wed Mar 7 20:25:01 CET 2012


How about, say, order()? Did you even try to look
in the help, under sort or order since both appear
as keywords in your question?

> dput(x)
structure(list(V1 = c(57L, 84L, 21L, 61L), V2 = c(91L, 3L, 6L,
16L), V3 = c(31L, 99L, 57L, 84L), V4 = c(61L, 85L, 91L, 3L),
    V5 = c(16L, 47L, 31L, 99L)), .Names = c("V1", "V2", "V3",
"V4", "V5"), class = "data.frame", row.names = c(NA, -4L))
> order(x[,1])
[1] 3 1 4 2
>
> x[order(x[,1]),]
  V1 V2 V3 V4 V5
3 21  6 57 91 31
1 57 91 31 61 16
4 61 16 84  3 99
2 84  3 99 85 47

Sarah


On Wed, Mar 7, 2012 at 2:20 PM, Ajay Askoolum <aa2e72e at yahoo.co.uk> wrote:
> be sorted by row in ascending/descending order?
>
> Given this matrix:
>
>  57 91 31 61 16
>  84  3 99 85 47
>  21  6 57 91 31
>  61 16 84  3 99
>
>
> I want to end with this:
>
>  21  6 57 91 31
>  57 91 31 61 16
>  61 16 84  3 99
>  84  3 99 85 47
>
>
> The 'order' of the sort is: 3 1 4 2
>
> Also, what R expression will give me the 'order'?
>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list