[R] How to extract rows from data frame based on unique variable groupings

Peter Alspach Peter.Alspach at plantandfood.co.nz
Thu May 20 22:23:04 CEST 2010


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.



More information about the R-help mailing list