Hi:

Here's one way:

d <- read.table(textConnection("
  ind  xloc  yloc  gonad
1   1    5     2     12
2   1    5     4     25
3   1    3     3     30
4   1    5     4     10
5   1    1     4     08
10  1    1     4     11"), header = TRUE)
closeAllConnections()

library(plyr)
dsum <- ddply(d, .(xloc, yloc), summarise, ind = sum(ind), gonad =
sum(gonad))

dsum[order(dsum$gonad, decreasing = TRUE), ][1:2, ]

  xloc yloc ind gonad
4    5    4   2    35
2    3    3   1    30

I hope this isn't homework....

Dennis


On Fri, Feb 25, 2011 at 1:01 PM, Nicolas Gutierrez <nicol...@uw.edu> wrote:

> Hi All,
>
> I've been doing circles with this all morning, although I'm sure there's a
> simple solution. I have a data.frame "pop" as follows:
>
> > pop
>
>   ind  xloc  yloc  gonad
> 1   1    5     2     12
> 2   1    5     4     25
> 3   1    3     3     30
> 4   1    5     4     10
> 5   1    1     4     08
> .   .    .     .      .
> 10  1    1     4     11
>
> And I need the following steps:
>
> 1. Sum "gonad" by combining (xloc, yloc) as follows and create data.frame
> "fish":
>
> > fish
>
>   ind  xloc  yloc  gonad
> 1   1     5     2     12
> 2   2     5     4     35
> 3   1     3     3     30
> 4   2     1     4     19
>
> 2. Extract the 2 highest values of "gonad":
>
> > new.fish
>
>   ind  xloc  yloc  gonad
> 1   2    5     4     35
> 2   1    3     3     31
>
> 3. Delete "fish" from "pop" (2 data.frames).
>
> Hope you get the idea and thanks for any help!!
>
> Nico
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to