I am trying to create a column that counts the number of unique visits to a site based on the grouping. However in my current operation is counting the same date as visit_3 and visit_4 because of having captures and recaptures. How do I not pick simple count the rows by the grouping but only by unique dates per site.
With my current process the last observation at "admin_pond on "2022-05-19" for the capture_type of "recapture" should have "visit_3" but it is showing at "visit_4". I want the unique number of visits per site and date irregardless of capture_type. So the last two values observations should show "visit_3" because they happened on the same date at the same site.
Data
data <- structure(list(site = c("admin_pond", "admin_pond", "admin_pond",
"admin_pond"), date = structure(c(19123, 19130, 19131, 19131), class = "Date"),
n = c(9L, 15L, 11L, 9L), capture_type = c("new", "new", "new",
"recapture")), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"))
Method
bull_frog_visits <- data %>%
group_by(site) %>%
mutate(n_visit = 1:n(),
n_visit = paste0("visit_", n_visit, sep = ""))
head(bull_frog_visits)