R - Tally for multiple factors in a column

1.9k Views Asked by At

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"))                    
2

There are 2 best solutions below

2
On BEST ANSWER

Get rid of the Capture column and use the dplyr library to aggregate

library(dplyr)

capture_tally <- events %>% group_by(Dive_id, Prey_present) %>% 
    summarise(Count_of_Captures = n())

It 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.

 Dive_id        Prey_present         Count_of_Captures
  <fctr>       <fctr>               <int>
1  dive.1                              1
2  dive.1       fish.a                 2
3  dive.1       fish.b                 3

EDIT: ok, try this.

library(tidyr); 

events %>% group_by(Dive_id, Prey_present) %>% 
   filter(Capture != "") %>%  # filter out captured ones (handling)
   summarise(Count = n()) %>%  #get the count for each fish type (long format)
   spread(Prey_present, Count) # Use the spread() function from tidyr package to convert the data from long to wide format

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.

1
On
library(dplyr)               
new1<- events %>% group_by(Dive_id,Prey_present) %>% summarise(Capture = NROW(Capture))

this will give you required output