Trying To learn again
2012-Jun-10 06:36 UTC
[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.
Özgür Asar
2012-Jun-10 08:25 UTC
[R] Order all the columns ascending elements on a matrix or a data frame
Try apply(b,2,sort) best ozgur -- View this message in context: http://r.789695.n4.nabble.com/Order-all-the-columns-ascending-elements-on-a-matrix-or-a-data-frame-tp4632922p4632924.html Sent from the R help mailing list archive at Nabble.com.
arun
2012-Jun-10 17:46 UTC
[R] Order all the columns ascending elements on a matrix or a data frame
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]),] ???????????? 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.
Bert Gunter
2012-Jun-10 20:47 UTC
[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
arun
2012-Jun-10 21:22 UTC
[R] Order all the columns ascending elements on a matrix or a data frame
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