Re: [R] sort a data frame by a vector

2009-12-02 Thread Hao Cen
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

Re: [R] sort a data frame by a vector

2009-12-01 Thread Don MacQueen
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

Re: [R] sort a data frame by a vector

2009-12-01 Thread David Winsemius
On Dec 1, 2009, at 10:59 PM, jim holtman wrote: The factor statement should have been: (missed the 'vec' on the first reading) dataDF$A1 <- factor(dataDF$A1, levels=vec) It's not necessary to alter the data.frame. You can use the results of the construction above as the row index and st

Re: [R] sort a data frame by a vector

2009-12-01 Thread jim holtman
The factor statement should have been: (missed the 'vec' on the first reading) dataDF$A1 <- factor(dataDF$A1, levels=vec) On Tue, Dec 1, 2009 at 10:57 PM, jim holtman wrote: > Is this what you want: > >> dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) >> dataDF >  A1 A2 > 1  B  1 > 2

Re: [R] sort a data frame by a vector

2009-12-01 Thread milton ruser
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

Re: [R] sort a data frame by a vector

2009-12-01 Thread jim holtman
Is this what you want: > dataDF = data.frame(A1 = c("B", "A", "C"), A2 = c(1,2,3)) > dataDF A1 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(dat