Aurelie Cosandey Godin
2011-Nov-06 19:23 UTC
[R] Combining some duplicated rows & summing one of their column
Dear list, I have this dataframe:> names(events)[1] "EID" "X" "Y" "trip" "tow" "catch" "effort" "depth" [9] "season" Where some of my unique ID "EID" appears more than once in 162 cases.> length(events$EID)-length(unique(events$EID))[1] 162 I would like to combined each replicate EID together and sum their "catch". I've been trying a few things with the plyr package... but can't find a rather straightforward command. Any tips would be much appreciated! Thank you very much! Aurelie Cosandey-Godin Ph.D. student, Department of Biology Industrial Graduate Fellow, WWF-Canada Dalhousie University | Email: godina@dal.ca [[alternative HTML version deleted]]
jim holtman
2011-Nov-06 20:00 UTC
[R] Combining some duplicated rows & summing one of their column
Since you did not supply a subset of data, or indicate if you wanted all the other values transformed in some ways, here is a simple use of tapply to get your data with respect to 'catch'. If this not what you wanted, you need to be more clear in your request and also give some hints as to what you tried.> x <- data.frame(EID = sample(10, 20, TRUE), catch = 1:20) > tapply(x$catch, x$EID, sum)1 2 3 4 5 6 7 8 9 10 18 12 20 5 54 19 20 4 56 2 On Sun, Nov 6, 2011 at 2:23 PM, Aurelie Cosandey Godin <godina at dal.ca> wrote:> Dear list, > > I have this dataframe: >> names(events) > ?[1] "EID" ? ?"X" ? ? ?"Y" ? ? ?"trip" ? "tow" ? ?"catch" ?"effort" "depth" > ?[9] "season" > Where some of my unique ID "EID" appears more than once in 162 cases. > >> length(events$EID)-length(unique(events$EID)) > [1] 162 > I would like to combined each replicate EID together and sum their "catch". I've been trying a few things with the plyr package... but can't find a rather straightforward command. > > Any tips would be much appreciated! Thank you very much! > > > > Aurelie Cosandey-Godin > Ph.D. student, Department of Biology > Industrial Graduate Fellow, WWF-Canada > Dalhousie University | Email: godina at dal.ca > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Maybe Matching Threads
- Export a plot/figure to excel or word?
- Deleting rows dataframe in R conditional to “if any of (a specific variable) is equal to”
- Removal/selecting specific rows in a dataframe conditional on 2 columns
- Polishing my geom_bar for publication
- Create a list object in a loop