[R] extract rows with unique values from data.frame

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Mar 8 05:16:26 CET 2011


Here is possibly one method (if I have understood you correctly):

> con <- textConnection("
+      xloc yloc  gonad  ind    Ene    W   Agent
+ 1    23  20   516.74   1     0.02 20.21 0.25
+ 2    23  20  1143.20   1     0.02 20.21 0.50
+ 3    21  19   250.00   1     0.02 20.21 0.25
+ 4    22  15   251.98   1     0.02 18.69 0.25
+ 5    24  18   598.08   1     0.02 18.69 0.25
+ ")
> 
> pop <- read.table(con, header = TRUE)
> close(con)
> 
> i <- with(pop, cumsum(!duplicated(cbind(xloc, yloc))))
> 
> k <- 2  ## how many do you want?
> 
> no <- min(which(i == k))
> pop[1:no, ]
  xloc yloc   gonad ind  Ene     W Agent
1   23   20  516.74   1 0.02 20.21  0.25
2   23   20 1143.20   1 0.02 20.21  0.50
3   21   19  250.00   1 0.02 20.21  0.25
> 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Nicolas Gutierrez
Sent: Tuesday, 8 March 2011 1:52 PM
To: R help
Subject: [R] extract rows with unique values from data.frame

Hello!

I have the data frame "pop":

     xloc yloc  gonad  ind    Ene    W   Agent
1    23  20   516.74   1     0.02 20.21 0.25
2    23  20  1143.20   1     0.02 20.21 0.50
3    21  19   250.00   1     0.02 20.21 0.25
4    22  15   251.98   1     0.02 18.69 0.25
5    24  18   598.08   1     0.02 18.69 0.25

And I need to extract the number of rows that have unique (xloc, yloc) 
values. For example I need 2 unique cells (xloc,yloc) so the number of 
rows to extract would be 3 as follows:

     xloc yloc  gonad  ind    Ene    W   Agent
1    23  20   516.74   1     0.02 20.21 0.25
2    23  20  1143.20   1     0.02 20.21 0.50
3    21  19   250.00   1     0.02 20.21 0.25

Thanks for any help!

Nico

______________________________________________
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