I have a sample data.frame, "events" which has multiple prey captures occurring on a single dive. Based on the Capture column, I have used the word "handling" to tally up the number of captures per dive.
However, in some instances I have multiple prey types in a single dive. How can I work out the number of prey captures based on species (i.e. how many fish.a and how many fish.b caught in a single dive)?
Any advice would be appreciated.
events <- data.frame(Prey_present =c("fish.a", "fish.a","", "fish.b",
"fish.b","fish.b"),
Capture = c("","","handling", "", "", "handling") ,
Dive_id =c("dive.1", "dive.1","dive.1", "dive.1","dive.1", "dive.1"))
temp<- tapply(events$Capture, events$Dive_id, function(x) rle(x ==
"handling"))
ncaptures<- data.frame(id = names(temp),
tally = unlist(lapply(temp, function(x) sum(x$values))))
final<-ncaptures[order(ncaptures$id),]
My final output (which I will bind to my bigger data.frame) should be something like:
final <- data.frame(fish.a =c(1),
fish.b = c(1),
Dive_id =c("dive.1"))
Get rid of the Capture column and use the
dplyr
library to aggregateIt will group by Dive_id and Prey_Present. then use the
summarise
function to perform the counts for each particular dive and prey type captured.You can name the
Count_of_Captures
column whatever you want.EDIT: Here's the output of the above code.
EDIT: ok, try this.
I'm guessing you're anytime the Capture Column is blank, no fish has been captured. and that you're counting only the instances it says
handling
. I might have misunderstood you again, so I apologize.