Dear all, Please suggest me how can I do it. I have a matrix which look like following: x1 x2 x3 t1 .01 0.3 0 t2 0 0.1 0.01 t3 0 .01 .01 t4 0 0 t5 5 0 0 t6 0 0 0 t7 0 0 0 t8 0 0 0 t9 0.6 0 0 t10 0 0 0.66 t11 0 0.6 0.11 t12 0 0.4 0 I want to sort decreasing order in each column based on rows. and then to display only those rows which has a value. The expected out put matrix will look like x1 x2 x3 t9 0.6 t11 0.6 t10 0.66 t1 .01 t12 0.4 t11 0.11 t1 0.3 t2 .01 t2 0.1 t3 .01 many thanks Nico [[alternative HTML version deleted]]
Perhaps you could supply the matrix using dput() ? It is a handy way to supply sample data. Just do dput(mydata), copy the results and paste into email. At the moment your matrix is almost unreadable. John Kane Kingston ON Canada> -----Original Message----- > From: nicomet80 at gmail.com > Sent: Wed, 29 Aug 2012 15:24:17 +0200 > To: r-help at r-project.org > Subject: [R] Sorting of columns of a matrix > > Dear all, > > Please suggest me how can I do it. > > > I have a matrix which look like following: > > x1 x2 x3 t1 .01 0.3 0 t2 0 0.1 0.01 t3 0 .01 .01 t4 0 0 t5 5 0 0 > t6 > 0 0 0 t7 0 0 0 t8 0 0 0 t9 0.6 0 0 t10 0 0 0.66 t11 0 0.6 0.11 t12 > 0 > 0.4 0 > > I want to sort decreasing order in each column based on rows. and then to > display only those rows which has a value. The expected out put matrix > will > look like > > x1 x2 x3 t9 0.6 t11 0.6 t10 0.66 t1 .01 t12 0.4 t11 0.11 t1 0.3 t2 > .01 > t2 0.1 t3 .01 > many thanks > > Nico > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!
Hi But in your example there are no row names? How would you structure the result? Each column has different order and therefore each column shall have different sequence of row names? Send also your responses to r-help as somebody could have better answer for you. Regards Petr From: Nico Met [mailto:nicomet80@gmail.com] Sent: Wednesday, August 29, 2012 4:56 PM To: PIKAL Petr Subject: Re: [R] Sorting of columns of a matrix Thanks a lot. But I also want to attach row names (if available) associated with each column. In that case does your second code works in the same way? thanks Animesh On Wed, Aug 29, 2012 at 4:46 PM, PIKAL Petr <petr.pikal@precheza.cz<mailto:petr.pikal@precheza.cz>> wrote: Hi> -----Original Message----- > From: r-help-bounces@r-project.org<mailto:r-help-bounces@r-project.org> [mailto:r-help-bounces@r-<mailto:r-help-bounces@r-> > project.org<http://project.org>] On Behalf Of Nico Met > Sent: Wednesday, August 29, 2012 4:25 PM > To: Berend Hasselman > Cc: r-help@r-project.org<mailto:r-help@r-project.org> > Subject: Re: [R] Sorting of columns of a matrix > > Please find the require info: > > > set.seed(12345) > X<-matrix(rnorm(5*10),nrow=5) ><snip>> > X[2,5]<-0 > X[3,1]<-0 > X[5,8]<-0<snip>> > dput(X) > > structure(c(0.585528817843856, 0.709466017509524, 0, - > 0.453497173462763, 0.605887455840393, -1.81795596770373, > 0.630098551068391, -0.276184105225216, -0.284159743943371, - > 0.919322002474128, -0.116247806352002, 1.81731204370422, > 0.370627864257954, 0.520216457554957, -0.750531994502331, > 0.816899839520583, -0.886357521243213, -0.331577589942552, > 1.12071265166956, 0.298723699267293, 0.779621924555324, 0, - > 0.644328429231302, -1.55313740522969, -1.59770951669631, > 1.80509751881082, -0.481647363694637, 0.620379801298422, > 0.612123492650849, -0.162310976918126, 0.81187317855386, > 2.19683354634753, 2.04919033740619, 1.63244563948047, > 0.254271192814055, 0.491188279272559, -0.324086578737118, - > 1.66205024385863, 1.76773385087297, 0, 1.12851083359018, - > 2.38035806139704, -1.06026555215253, 0.937140540182908, > 0.854451720330554, 1.46072940310409, -1.4130987778892, > 0.567403253424482, 0.583187653435685, -1.30679883346442), .Dim = c(5L, > 10L)) >I am not sure what do you want to do with it? Remove rows which has 0 X[X==0]<-NA X[complete.cases(X),] remove zeroes and sort values independently in each column? X[X==0]<-NA apply(X, 2, sort, decreasing=TRUE) results in list as you have various number of values in columns. Regards Petr> > On Wed, Aug 29, 2012 at 4:16 PM, Berend Hasselman <bhh@xs4all.nl<mailto:bhh@xs4all.nl>> > wrote: > > > > > On 29-08-2012, at 16:08, Nico Met wrote: > > > > > Hello john, > > > > > > thanks for the suggestion. Please find an example: > > > > > >> X<-matrix(rnorm(5*10),nrow=5) > > > > > >> dim(X) > > > [1] 5 10 > > >> X > > > [,1] [,2] [,3] [,4] [,5] > [,6] > > > [,7] [,8] [,9] [,10] > > > [1,] 1.2774431 -1.2427735 0.81933548 -2.1098586 -1.6726799 > > > -2.2994684 > > > -0.28823228 0.1929301 0.1187168 -1.2078372 [2,] 0.5969187 > > > -1.2571336 0.30292368 -0.2098139 -0.9689961 1.7105109 > > > 0.77240398 0.1482712 -1.2486152 0.2359723 [3,] -0.9669289 > > > -0.2315229 1.04611417 0.7902772 0.5791674 0.6296305 > > > -0.60517647 0.5317435 -0.2556417 -0.4838956 [4,] -1.9345400 > > > 0.2898647 0.08983352 0.1482130 0.8229054 0.7778114 > > > 0.49423641 0.4015327 2.8596603 0.6618432 [5,] -1.1969226 > > > -1.3991657 -0.76507258 0.5024922 0.2633307 0.0236774 > > > -0.09497448 -0.3986624 0.1327156 -0.8935923 > > > > > >> X[2,5]<-0 > > >> X[3,1]<-0 > > >> X[6,8]<-0 > > >> X[5,8]<-0 > > >> X > > > [,1] [,2] [,3] [,4] [,5] > [,6] > > > [,7] [,8] [,9] [,10] > > > [1,] 1.2774431 -1.2427735 0.81933548 -2.1098586 -1.6726799 > > > -2.2994684 > > > -0.28823228 0.1929301 0.1187168 -1.2078372 [2,] 0.5969187 > > > -1.2571336 0.30292368 -0.2098139 0.0000000 1.7105109 > > > 0.77240398 0.1482712 -1.2486152 0.2359723 [3,] 0.0000000 > > > -0.2315229 1.04611417 0.7902772 0.5791674 0.6296305 > > > -0.60517647 0.5317435 -0.2556417 -0.4838956 [4,] -1.9345400 > > > 0.2898647 0.08983352 0.1482130 0.8229054 0.7778114 > > > 0.49423641 0.4015327 2.8596603 0.6618432 [5,] -1.1969226 > > > -1.3991657 -0.76507258 0.5024922 0.2633307 0.0236774 > > > -0.09497448 0.0000000 0.1327156 -0.8935923 > > > > This is just as unreadable as your first try. > > Please do as suggested > > > > dput(X) > > > > and also include a set.seed() before generating the matrix to make it > > reproducible. > > > > Berend > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org<mailto:R-help@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.[[alternative HTML version deleted]]
HI, Not sure I understand the last part. Try this: dat1<-structure(c(0.585528817843856, 0.709466017509524, 0, -0.453497173462763, 0.605887455840393, -1.81795596770373, 0.630098551068391, -0.276184105225216, -0.284159743943371, -0.919322002474128, -0.116247806352002, 1.81731204370422, 0.370627864257954, 0.520216457554957, -0.750531994502331, 0.816899839520583, -0.886357521243213, -0.331577589942552, 1.12071265166956, 0.298723699267293, 0.779621924555324, 0, -0.644328429231302, -1.55313740522969, -1.59770951669631, 1.80509751881082, -0.481647363694637, 0.620379801298422, 0.612123492650849, -0.162310976918126, 0.81187317855386, 2.19683354634753, 2.04919033740619, 1.63244563948047, 0.254271192814055, 0.491188279272559, -0.324086578737118, -1.66205024385863, 1.76773385087297, 0, 1.12851083359018, -2.38035806139704, -1.06026555215253, 0.937140540182908, 0.854451720330554, 1.46072940310409, -1.4130987778892, 0.567403253424482, 0.583187653435685, -1.30679883346442), .Dim = c(5L, 10L)) ?colnames(dat1)<-paste0("x",1:10) apply(dat1,2,sort,decreasing=TRUE) ???????????? x1???????? x2???????? x3???????? x4???????? x5???????? x6 #[1,]? 0.7094660? 0.6300986? 1.8173120? 1.1207127? 0.7796219? 1.8050975 #[2,]? 0.6058875 -0.2761841? 0.5202165? 0.8168998? 0.0000000? 0.6203798 #[3,]? 0.5855288 -0.2841597? 0.3706279? 0.2987237 -0.6443284? 0.6121235 #[4,]? 0.0000000 -0.9193220 -0.1162478 -0.3315776 -1.5531374 -0.1623110 #[5,] -0.4534972 -1.8179560 -0.7505320 -0.8863575 -1.5977095 -0.4816474 ??????????? x7???????? x8???????? x9??????? x10 #[1,] 2.1968335? 1.7677339? 1.1285108? 1.4607294 #[2,] 2.0491903? 0.4911883? 0.9371405? 0.5831877 #[3,] 1.6324456? 0.0000000? 0.8544517? 0.5674033 #[4,] 0.8118732 -0.3240866 -1.0602656 -1.3067988 #[5,] 0.2542712 -1.6620502 -2.3803581 -1.4130988 A.K. ----- Original Message ----- From: Nico Met <nicomet80 at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, August 29, 2012 9:24 AM Subject: [R] Sorting of columns of a matrix Dear all, Please suggest me how can I do it. I have a matrix which look like following: ? x1 x2 x3? t1 .01 0.3 0? t2 0 0.1 0.01? t3 0 .01 .01? t4 0 0? t5 5 0 0? t6 0 0 0? t7 0 0 0? t8 0 0 0? t9 0.6 0 0? t10 0 0 0.66? t11 0 0.6 0.11? t12 0 0.4 0 I want to sort decreasing order in each column based on rows. and then to display only those rows which has a value. The expected out put matrix will look like ? x1 x2 x3? t9 0.6 t11 0.6 t10 0.66? t1 .01 t12 0.4 t11 0.11? t1 0.3 t2 .01 t2 0.1? ? t3 .01 many thanks Nico ??? [[alternative HTML version deleted]] ______________________________________________ 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.
HI, I guess this might be what you want. dat1<-structure(c(0.585528817843856, 0.709466017509524, 0, -0.453497173462763, 0.605887455840393, -1.81795596770373, 0.630098551068391, -0.276184105225216, -0.284159743943371, -0.919322002474128, -0.116247806352002, 1.81731204370422, 0.370627864257954, 0.520216457554957, -0.750531994502331, 0.816899839520583, -0.886357521243213, -0.331577589942552, 1.12071265166956, 0.298723699267293, 0.779621924555324, 0, -0.644328429231302, -1.55313740522969, -1.59770951669631, 1.80509751881082, -0.481647363694637, 0.620379801298422, 0.612123492650849, -0.162310976918126, 0.81187317855386, 2.19683354634753, 2.04919033740619, 1.63244563948047, 0.254271192814055, 0.491188279272559, -0.324086578737118, -1.66205024385863, 1.76773385087297, 0, 1.12851083359018, -2.38035806139704, -1.06026555215253, 0.937140540182908, 0.854451720330554, 1.46072940310409, -1.4130987778892, 0.567403253424482, 0.583187653435685, -1.30679883346442), .Dim = c(5L, 10L)) ?colnames(dat1)<-paste0("x",1:10) dat2<-apply(dat1,2,sort,decreasing=TRUE) dat2[-which(rowSums(dat2==0)>0),] ? # ????????? x1???????? x2??????? x3???????? x4???????? x5???????? x6??????? x7 #[1,]? 0.7094660? 0.6300986? 1.817312? 1.1207127? 0.7796219? 1.8050975 2.1968335 #[2,] -0.4534972 -1.8179560 -0.750532 -0.8863575 -1.5977095 -0.4816474 0.2542712 ? # ???????? x8??????? x9?????? x10 #[1,]? 1.767734? 1.128511? 1.460729 #[2,] -1.662050 -2.380358 -1.413099 A.K. ----- Original Message ----- From: Nico Met <nicomet80 at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, August 29, 2012 9:24 AM Subject: [R] Sorting of columns of a matrix Dear all, Please suggest me how can I do it. I have a matrix which look like following: ? x1 x2 x3? t1 .01 0.3 0? t2 0 0.1 0.01? t3 0 .01 .01? t4 0 0? t5 5 0 0? t6 0 0 0? t7 0 0 0? t8 0 0 0? t9 0.6 0 0? t10 0 0 0.66? t11 0 0.6 0.11? t12 0 0.4 0 I want to sort decreasing order in each column based on rows. and then to display only those rows which has a value. The expected out put matrix will look like ? x1 x2 x3? t9 0.6 t11 0.6 t10 0.66? t1 .01 t12 0.4 t11 0.11? t1 0.3 t2 .01 t2 0.1? ? t3 .01 many thanks Nico ??? [[alternative HTML version deleted]] ______________________________________________ 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.