[R] Order all the columns ascending elements on a matrix or a data frame
arun
smartpink111 at yahoo.com
Sun Jun 10 23:22:52 CEST 2012
Hi Bert,
I tried the code.
dat2<-data.frame(dat1)
> do.call(order,dat2)
[1] 3 6 1 10 2 5 7 9 8 4
Here, I get the order of 1st column as a list. Is there anything I am missing here?
Thanks,
A.K.
----- Original Message -----
From: Bert Gunter <gunter.berton at gene.com>
To: arun <smartpink111 at yahoo.com>
Cc: Trying To learn again <tryingtolearnagain at gmail.com>; R help <r-help at r-project.org>
Sent: Sunday, June 10, 2012 4:47 PM
Subject: Re: [R] Order all the columns ascending elements on a matrix or a data frame
Inline ...
-- Bert
On Sun, Jun 10, 2012 at 10:46 AM, arun <smartpink111 at yahoo.com> wrote:
> Hi,
>
> If your intention is to order the first column by ascending, then by 2nd and so on..
> Try this.
>
> set.seed(1)
> dat1<-cbind(x=rnorm(10,5,0.5),y=runif(10,0.4),z=rnorm(10,15,0.2))
> dat1
> x y z
> [1,] 4.686773 0.9608231 14.99101
> [2,] 5.091822 0.5272855 14.99676
> [3,] 4.582186 0.7910043 15.18877
> [4,] 5.797640 0.4753331 15.16424
> [5,] 5.164754 0.5603324 15.11878
> [6,] 4.589766 0.6316685 15.18380
> [7,] 5.243715 0.4080342 15.15643
> [8,] 5.369162 0.6294328 15.01491
> [9,] 5.287891 0.9218145 14.60213
> [10,] 4.847306 0.6042094 15.12397
>
> dat1[order(dat1[,1],dat1[,2],dat1[,3]),]
## if dat1 is a data frame, e.g.
dat1 <- data.frame(dat1)
## This can be shortened to:
do.call(order,dat1)
?do.call
-- Bert
> x y z
> [1,] 4.582186 0.7910043 15.18877
> [2,] 4.589766 0.6316685 15.18380
> [3,] 4.686773 0.9608231 14.99101
> [4,] 4.847306 0.6042094 15.12397
> [5,] 5.091822 0.5272855 14.99676
> [6,] 5.164754 0.5603324 15.11878
> [7,] 5.243715 0.4080342 15.15643
> [8,] 5.287891 0.9218145 14.60213
> [9,] 5.369162 0.6294328 15.01491
> [10,] 5.797640 0.4753331 15.16424
>
>
>
> But, if it is like to order all the columns at once,
>
> apply(dat1,2,sort)
>
> x y z
> [1,] 4.582186 0.4080342 14.60213
> [2,] 4.589766 0.4753331 14.99101
> [3,] 4.686773 0.5272855 14.99676
> [4,] 4.847306 0.5603324 15.01491
> [5,] 5.091822 0.6042094 15.11878
> [6,] 5.164754 0.6294328 15.12397
> [7,] 5.243715 0.6316685 15.15643
> [8,] 5.287891 0.7910043 15.16424
> [9,] 5.369162 0.9218145 15.18380
> [10,] 5.797640 0.9608231 15.18877
>
> Here, the all columns are sorted to ascending, but only problem is that the corresponding elements in each of the rows in the original dataset has also changed.
>
>
> A.K.
>
>
>
>
> ----- Original Message -----
> From: Trying To learn again <tryingtolearnagain at gmail.com>
> To: r-help at r-project.org
> Cc:
> Sent: Sunday, June 10, 2012 2:36 AM
> Subject: [R] Order all the columns ascending elements on a matrix or a data frame
>
> Imagine I have a csv KT.csv
>
> I want to create a new dataframe o convert KT in a matrix and create a new
> matrix with each column of KT ordered by ascending order.
>
> I have tried to make this
>
> b<-read.csv("KT.csv")
>
>
> for(i in 1:ncol(b)){
>
> b[,i]<-sort(b[,i])
>
> }
>
> But it puts a message that the number of rows doesn´t correspond.
>
> Can someone give me a clue on how to order.
>
> Many Thaks.
>
> ______________________________________________
> 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.
>
>
> ______________________________________________
> 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.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list