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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Michael Hosack


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
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to extract rows from data frame based on unique variable groupings

Peter Alspach-2
Tena koe Mike

duplicated() might be what you require.

HTH ...

Peter Alspach

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Michael H
> Sent: Friday, 21 May 2010 6:37 a.m.
> To: [hidden email]
> 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
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to extract rows from data frame based on unique variable groupings

Wu Gong
In reply to this post by Michael Hosack
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),])