Hi all, I've got a data frame that has an identical column to a chr vector. I would like to use the chr vector to order the rows of the data frame to be identical to the order in the chr vector (the contents of the chr vector are completely identical to one col of the data frame), but this is proving trickier than it sounds.. Any help would be much obliged, -Ken
Let me clarify the problem a bit further: tv <- 1:11108 tdf <- data.frame(cbind(11108:1, 22216:11109)) In this example, what I would like to do is re-order the entire tdf data frame based on its first column (since the contents of tdf[,1] are identical to tv, just not necessarily in the same order in my problem, but here obviously the tdf data frame is already ordered the same as tv). I would like to use the vector tv to give tdf the same exact order as tv, based on that first column of tdf which is identical to tv. Thanks, ken
This is an FAQ that's not in the official FAQ (I believe). Do RSiteSearch("sort.data.frame") in R and you'll see. Andy From: Ken Termiso> > Hi all, > > I've got a data frame that has an identical column to a chr > vector. I would > like to use the chr vector to order the rows of the data frame to be > identical to the order in the chr vector (the contents of the > chr vector are > completely identical to one col of the data frame), but this > is proving > trickier than it sounds.. > > Any help would be much obliged, > -Ken > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Dimitris Rizopoulos
2006-Jan-23 16:13 UTC
[R] ordering a data frame to same order as a chr vector
probably you are looking for something like the following: dat <- data.frame(chr = letters[1:20], x = rnorm(20), y = rnorm(20)) chr.vec <- sample(letters[1:20]) ####################3 dat[match(chr.vec, dat$chr), ] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Ken Termiso" <jerk_alert at hotmail.com> To: <r-help at stat.math.ethz.ch> Sent: Monday, January 23, 2006 4:55 PM Subject: [R] ordering a data frame to same order as a chr vector> Hi all, > > I've got a data frame that has an identical column to a chr vector. > I would > like to use the chr vector to order the rows of the data frame to be > identical to the order in the chr vector (the contents of the chr > vector are > completely identical to one col of the data frame), but this is > proving > trickier than it sounds.. > > Any help would be much obliged, > -Ken > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm