Michael H
2010-May-20 18:36 UTC
[R] How to extract rows from data frame based on unique variable groupings
R community, I would like to know how to extract rows from a data frame (DF) such that each row in the new data frame (D.F) represents the first instance of a unique variable pairing in the original dataframe (ordered first by variable V1 then by variable V2). The unique function does not seem to be able to accomplish this. I imagine there must be a simple solution for this, but I can't seem to find it. Thank you, Mike DF <- structure(list(V1 = c(1, 1, 1, 1, 2, 2, 2, 2), V2 = c(1, 1, 2, 2, 1, 1, 2, 2), V3 = c(1L, 8L, 4L, 3L, 6L, 2L, 7L, 5L), V4 = c(8L, 3L, 6L, 1L, 2L, 4L, 5L, 7L)), .Names = c("V1", "V2", "V3", "V4"), row.names = c(NA, -8L), class = "data.frame") D.F <- structure(list(V.1 = c(1, 1, 2, 2), V.2 = c(1, 2, 1, 2), V.3 = c(1, 4, 6, 7), V.4 = c(8, 6, 2, 5)), .Names = c("V.1", "V.2", "V.3", "V.4"), row.names = c(NA, -4L), class = "data.frame") _________________________________________________________________ Hotmail is redefining busy with tools for the New Busy. Get more from your inbox. N:WL:en-US:WM_HMP:042010_2
Peter Alspach
2010-May-20 20:23 UTC
[R] How to extract rows from data frame based on unique variable groupings
Tena koe Mike duplicated() might be what you require. HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Michael H > Sent: Friday, 21 May 2010 6:37 a.m. > To: r-help at r-project.org > Subject: [R] How to extract rows from data frame based on unique > variable groupings > > > > R community, > > I would like to know how to extract rows from a data frame (DF) such > that > each row in the new data frame (D.F) represents the first instance of > a unique variable pairing in the original dataframe (ordered first by > variable V1 then by variable V2). The unique function does not seem > to be able to accomplish this. I imagine there must be a simple > solution for this, > but I can't seem to find it. > > Thank you, > > Mike > > > DF <- > structure(list(V1 = c(1, 1, 1, 1, 2, 2, 2, 2), V2 = c(1, 1, 2, 2, 1,1,> 2, 2), > V3 = c(1L, 8L, 4L, 3L, 6L, 2L, 7L, 5L), V4 = c(8L, 3L, 6L, 1L, 2L, 4L, > 5L, 7L)), > .Names = c("V1", "V2", "V3", "V4"), row.names = c(NA, -8L), class > "data.frame") > > > D.F <- > structure(list(V.1 = c(1, 1, 2, 2), V.2 = c(1, 2, 1, 2), V.3 = c(1, 4, > 6, 7), > V.4 = c(8, 6, 2, 5)), .Names = c("V.1", "V.2", "V.3", "V.4"),row.names> = c(NA, -4L), > class = "data.frame") > _________________________________________________________________ > Hotmail is redefining busy with tools for the New Busy. Get more from > your inbox. > > N:WL:en-US:WM_HMP:042010_2 > ______________________________________________ > 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.
Wu Gong
2010-May-20 22:26 UTC
[R] How to extract rows from data frame based on unique variable groupings
I hope this is what you want. ## Exclude replicated rows (DF1 <- unique(DF)) ## Sort the data (DF2 <- DF1[order(DF1$V1, DF1$V2, DF1$V3, DF1$V4),]) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/How-to-extract-rows-from-data-frame-based-on-unique-variable-groupings-tp2225233p2225410.html Sent from the R help mailing list archive at Nabble.com.