我有一个示例 data.frame,“事件”,它在一次潜水中发生了多个猎物捕获。根据捕获列,我使用了“处理”这个词来计算每次潜水的捕获次数。
然而,在某些情况下,我在一次潜水中有多种猎物类型。我如何计算基于物种捕获的猎物数量(即一次潜水捕获了多少个fish.a 和多少个fish.b)?
任何意见,将不胜感激。
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),]
我的最终输出(我将绑定到我更大的 data.frame)应该是这样的:
final <- data.frame(fish.a =c(1),
fish.b = c(1),
Dive_id =c("dive.1"))