Ho questo dataframe:
> names(events)
[1] "EID" "X" "Y" "trip" "tow" "catch" "effort" "depth"
[9] "season"
Dove alcuni dei miei ID univoci "EID" appaiono più di una volta in 162 casi.
> length(events$EID)-length(unique(events$EID))
[1] 162
Vorrei combinare ogni EID replicare insieme e riassumere la loro "presa". Ho provato alcune cose con il pacchetto plyr ... ma non riesco a trovare un comando piuttosto semplice.
Ogni suggerimento e 'ben accetto! Grazie mille!
risposte:
3 per risposta № 1Ecco un esempio che dovrebbe iniziare:
library(plyr)
n = 100
# Simulate data
set.seed(12345)
EID = factor(sample(10, n, replace=T))
catch = runif(n)
data = data.frame(EID, catch)
# Sum catch by EID
ddply(data, "EID", function(x) data.frame(catch.sum = sum(x$catch)))
> ddply(data, "EID", function(x) data.frame(catch.sum = sum(x$catch)))
EID catch.sum
1 1 6.041294
2 2 4.889522
3 3 2.466826
4 4 9.633270
5 5 5.673484
6 6 3.389666
7 7 6.572664
8 8 8.646577
9 9 4.056461
10 10 5.933441