Dear all, I am looking for a suggestion please regarding sorting a dataframe with alphanumerical components : let's assume we have : A = c("A1","A10","A11","A2") B = c(1,2,3,4) C = data.frame(A,B) how could I sort C data.frame in such a way that we have at the end : C$A in the order : "A1", "A2", "A10", "A11". thank you very much, -- bogdan [[alternative HTML version deleted]]
On Jun 20, 2015, at 5:18 PM, Bogdan Tanasa wrote:> Dear all, > > I am looking for a suggestion please regarding sorting a dataframe with > alphanumerical components : > > let's assume we have : > > A = c("A1","A10","A11","A2") > B = c(1,2,3,4) > > C = data.frame(A,B) > > how could I sort C data.frame in such a way that we have at the end : > > C$A in the order : "A1", "A2", "A10", "A11". thank you very much,Do a search on `mixedorder` and` mixedsort`. They are function names in pkg:gtools -- David Winsemius Alameda, CA, USA
Bogdan, Follow my suggestion. letter <- substring(A, 1, 1) number <- substring(A, 2, nchar(A)) new.data <- paste0(letter, formatC(as.numeric(number), width = 2, flag "0")) Em 20/06/2015 21:21, "Bogdan Tanasa" <tanasa at gmail.com> escreveu:> Dear all, > > I am looking for a suggestion please regarding sorting a dataframe with > alphanumerical components : > > let's assume we have : > > A = c("A1","A10","A11","A2") > B = c(1,2,3,4) > > C = data.frame(A,B) > > how could I sort C data.frame in such a way that we have at the end : > > C$A in the order : "A1", "A2", "A10", "A11". thank you very much, > > -- bogdan > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
thank you all, it is working fine. happy weekend ;) ! On Sat, Jun 20, 2015 at 6:15 PM, David Winsemius <dwinsemius at comcast.net> wrote:> > On Jun 20, 2015, at 5:18 PM, Bogdan Tanasa wrote: > > > Dear all, > > > > I am looking for a suggestion please regarding sorting a dataframe with > > alphanumerical components : > > > > let's assume we have : > > > > A = c("A1","A10","A11","A2") > > B = c(1,2,3,4) > > > > C = data.frame(A,B) > > > > how could I sort C data.frame in such a way that we have at the end : > > > > C$A in the order : "A1", "A2", "A10", "A11". thank you very much, > > Do a search on `mixedorder` and` mixedsort`. They are function names in > pkg:gtools > > -- > > David Winsemius > Alameda, CA, USA > >[[alternative HTML version deleted]]
Diego: Nonsense! Look at the results of your code -- you have failed to order the results as had been requested by the OP. It's also unnecessarily complicated. The following suffices (where I have used regular expressions rather substring() to get the numeric part of the strings -- **assuming** that the strings always consist of letters followed by numeric digits).> A = c("A1","A10","A11","A2") > B = c(1,2,3,4) > dat = data.frame(A,B) > > numbs <- as.numeric(gsub("[^[:digit:]]+","",dat$A)) > newdat <- dat[order(numbs),] > newdatA B 1 A1 1 4 A2 4 2 A10 2 3 A11 3 Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Sat, Jun 20, 2015 at 6:29 PM, Diego Miro <d.miro1089 at gmail.com> wrote:> Bogdan, > > Follow my suggestion. > > letter <- substring(A, 1, 1) > number <- substring(A, 2, nchar(A)) > new.data <- paste0(letter, formatC(as.numeric(number), width = 2, flag > "0")) > Em 20/06/2015 21:21, "Bogdan Tanasa" <tanasa at gmail.com> escreveu: > >> Dear all, >> >> I am looking for a suggestion please regarding sorting a dataframe with >> alphanumerical components : >> >> let's assume we have : >> >> A = c("A1","A10","A11","A2") >> B = c(1,2,3,4) >> >> C = data.frame(A,B) >> >> how could I sort C data.frame in such a way that we have at the end : >> >> C$A in the order : "A1", "A2", "A10", "A11". thank you very much, >> >> -- bogdan >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.