HI, I have searched the R-help achive, trying to find a way to sort columns in a data frame according to a specific list, but not be able to find a general way to do it. My data frame contains columns, with each one named from a sample ID, and each sample was measured in a specific time, generating one column of data, now I would like to sort the columns according to the time course, so I have the second data frame containing sample IDs with time point, but how should I do the sorting? Thanks a lot! Jie [[alternative HTML version deleted]]
So your second dataframe, say DF2, has sample IDs in one of its columns, say "colnames", (or are they in in the names?) ( Examples and output of str() are requested to avoid exactly such ambiguity.) If it is the former, then; DF1[ , DF2colnames ] # could do it. -- David Winsemius On Mar 16, 2009, at 4:41 PM, Jie Zhang wrote:> HI, > > I have searched the R-help achive, trying to find a way to sort > columns in a data frame according to a specific list, but not be > able to find a general way to do it. > > My data frame contains columns, with each one named from a sample > ID, and each sample was measured in a specific time, generating one > column of data, now I would like to sort the columns according to > the time course, so I have the second data frame containing sample > IDs with time point, but how should I do the sorting? > > Thanks a lot! > > Jie > > > > [[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.David Winsemius, MD Heritage Laboratories West Hartford, CT
On Mar 16, 2009, at 5:05 PM, David Winsemius wrote:> So your second dataframe, say DF2, has sample IDs in one of its > columns, say "colnames", (or are they in in the names?) > > ( Examples and output of str() are requested to avoid exactly such > ambiguity.) > > If it is the former, then; > > DF1[ , DF2colnames ] # could do it.^ DF1[ , DF2$colnames ]> > > -- > David Winsemius > > On Mar 16, 2009, at 4:41 PM, Jie Zhang wrote: > >> HI, >> >> I have searched the R-help achive, trying to find a way to sort >> columns in a data frame according to a specific list, but not be >> able to find a general way to do it. >> >> My data frame contains columns, with each one named from a sample >> ID, and each sample was measured in a specific time, generating one >> column of data, now I would like to sort the columns according to >> the time course, so I have the second data frame containing sample >> IDs with time point, but how should I do the sorting? >> >> Thanks a lot! >> >> Jie >> > >David Winsemius, MD Heritage Laboratories West Hartford, CT
Jie, df[match(df.t$ID,df$ID),] should work. df.t$ID is your target sequence. df$ID is your current sequence. Jun On Mon, Mar 16, 2009 at 3:41 PM, Jie Zhang <jieuiuc@yahoo.com> wrote:> HI, > > I have searched the R-help achive, trying to find a way to sort columns in > a data frame according to a specific list, but not be able to find a general > way to do it. > > My data frame contains columns, with each one named from a sample ID, and > each sample was measured in a specific time, generating one column of > data, now I would like to sort the columns according to the time course, so > I have the second data frame containing sample IDs with time point, but how > should I do the sorting? > > Thanks a lot! > > Jie > > > > [[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 > and provide commented, minimal, self-contained, reproducible code. > >-- Jun Shen PhD PK/PD Scientist BioPharma Services Millipore Corporation 15 Research Park Dr. St Charles, MO 63304 Direct: 636-720-1589 [[alternative HTML version deleted]]