Hi, I have a a vector and a data frame with two columns vec = c("C", "A", "B") dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) I would like to sort the data frame by column A1 such that the order of elements in A1 is as the same as in vec. After the ordering, the data frame would be A1 A2 C 3 A 2 B 1 Any suggestions would be appreciated. Thanks in advance Jeff [[alternative HTML version deleted]]
Is this what you want:> dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) > dataDFA1 A2 1 B 1 2 A 2 3 C 3> dataDF[order(dataDF$A1),]A1 A2 2 A 2 1 B 1 3 C 3>If you want the sequence "CAB" then you will have to change the factors in column 1:> dataDF$A1 <- factor(dataDF$A1, levels=c("C", "A", "B")) > dataDF[order(dataDF$A1),]A1 A2 3 C 3 2 A 2 1 B 1>On Tue, Dec 1, 2009 at 10:36 PM, Hao Cen <hcen at andrew.cmu.edu> wrote:> Hi, > > > > I have a a vector ?and a data frame with two columns > > vec = c("C", "A", "B") > > dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) > > > > I would like to sort the data frame by column A1 such that the order of > elements in A1 is as the same as in vec. > > > > After the ordering, the data frame would be > > A1 ? ? ? ? ? A2 > > C ? ? ? ? ? ? 3 > > A ? ? ? ? ? ? 2 > > B ? ? ? ? ? ? 1 > > > > Any suggestions would be appreciated. > > > > Thanks in advance > > > > Jeff > > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
not ellegant.. but... vecDF = data.frame(A1=c("C", "A", "B")) vecDF$A1.order=1:dim(vecDF) vecDF dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) dataDF2<-merge(vecDF, dataDF, by=intersect(colnames(vecDF),colnames(dataDF))) dataDF2 dataDF2.ord<-dataDF2[order(dataDF2$A1.order),] dataDF2.ord milton On Tue, Dec 1, 2009 at 10:36 PM, Hao Cen <hcen@andrew.cmu.edu> wrote:> Hi, > > > > I have a a vector and a data frame with two columns > > vec = c("C", "A", "B") > > dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) > > > > I would like to sort the data frame by column A1 such that the order of > elements in A1 is as the same as in vec. > > > > After the ordering, the data frame would be > > A1 A2 > > C 3 > > A 2 > > B 1 > > > > Any suggestions would be appreciated. > > > > Thanks in advance > > > > Jeff > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
This looks like a job for match().> vec = c("C", "A", "B") > > dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) > > > dataDF[match(dataDF$A1,vec),]A1 A2 3 C 3 2 A 2 1 B 1 -Don At 10:36 PM -0500 12/1/09, Hao Cen wrote:>Hi, > > > >I have a a vector and a data frame with two columns > >vec = c("C", "A", "B") > >dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) > > > >I would like to sort the data frame by column A1 such that the order of >elements in A1 is as the same as in vec. > > > >After the ordering, the data frame would be > >A1 A2 > >C 3 > >A 2 > >B 1 > > > >Any suggestions would be appreciated. > > > >Thanks in advance > > > >Jeff > > > [[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.-- --------------------------------- Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 macq at llnl.gov
Thanks to all who replied. Issue solved Jeff On Wed, December 2, 2009 12:52 am, Don MacQueen wrote:> This looks like a job for match(). > > >> vec = c("C", "A", "B") >> >> dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) >> >> >> dataDF[match(dataDF$A1,vec),] > A1 A2 > 3 C 3 > 2 A 2 > 1 B 1 > > > -Don > > > > At 10:36 PM -0500 12/1/09, Hao Cen wrote: > >> Hi, >> >> >> >> >> I have a a vector and a data frame with two columns >> >> >> vec = c("C", "A", "B") >> >> dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) >> >> >> >> I would like to sort the data frame by column A1 such that the order of >> elements in A1 is as the same as in vec. >> >> >> >> After the ordering, the data frame would be >> >> >> A1 A2 >> >> >> C 3 >> >> >> A 2 >> >> >> B 1 >> >> >> >> >> Any suggestions would be appreciated. >> >> >> >> >> Thanks in advance >> >> >> >> >> Jeff >> >> >> >> [[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. > > > -- > --------------------------------- > Don MacQueen > Lawrence Livermore National Laboratory > Livermore, CA, USA > 925-423-1062 > macq at llnl.gov --------------------------------- > > >