I have a data frame with individuals and which site they were detected.
ID Location
5994 Greshornish
5994 Snizort
6017 Greshornish
6018 Greshornish
6020 Greshornish
6020 Snizort
6021 Greshornish
46507 Greshornish
46508 Snizort
46511 Greshornish
46512 Greshornish
46512 Snizort
I would like to count the number of individuals detected at one site or both. For example, in the table above I would get something like
Location Count
Greshornish 5
Snizort 1
Both 3
I can't figure out the best way to do this although I'm sure it's quite simple.
CodePudding user response:
library(dplyr)
df %>%
group_by(ID) %>%
summarise(Location = ifelse(n() == 2, "Both", Location)) %>%
count(Location, name = "Count")
# Location Count
# 1 Both 3
# 2 Greshornish 5
# 3 Snizort 1
CodePudding user response:
This should work:)
library(dplyr)
df %>% group_by(Location) %>% tally() %>% rename(Count = n)