|
| 1 | +library(dplyr, warn.conflicts=FALSE, quietly = TRUE) |
| 2 | + |
| 3 | +# Assigns unique individual IDs to new rodent data |
| 4 | +# modified from clean_tags below to work on monthly data |
| 5 | +# |
| 6 | +# |
| 7 | +add_id <- function(raw_data, rodent_data, new_period){ |
| 8 | + |
| 9 | + rodent_data <- rodent_data %>% |
| 10 | + filter(period<new_period,period>=new_period-36) |
| 11 | + |
| 12 | + # get all ids |
| 13 | + all_ids <- rodent_data %>% |
| 14 | + select(species,tag,pit_tag,id) %>% |
| 15 | + distinct() |
| 16 | + |
| 17 | + # get existing tags |
| 18 | + current_tags <- rodent_data %>% |
| 19 | + filter(pit_tag==TRUE) %>% |
| 20 | + select(tag,id) %>% |
| 21 | + distinct() |
| 22 | + |
| 23 | + # append the tag type |
| 24 | + raw_data$pit_tag <- PIT_tag(raw_data$tag, raw_data$species) |
| 25 | + |
| 26 | + # add PIT tag-based ids, one day at a time (for pesky weekend recaptures) |
| 27 | + new_data1 <- raw_data %>% |
| 28 | + filter(day == min(day,na.rm = TRUE)) %>% |
| 29 | + left_join(current_tags, by="tag") %>% |
| 30 | + mutate(id = case_when(pit_tag==TRUE & is.na(id) ~ paste0(tag, "_", species, "_1") , |
| 31 | + TRUE ~ id)) |
| 32 | + current_tags <- rbind(current_tags,new_data1[!is.na(new_data1$tag),c(20,31)]) %>% |
| 33 | + distinct() |
| 34 | + |
| 35 | + new_data2 <- raw_data %>% |
| 36 | + filter(!(day %in% new_data1$day)) %>% |
| 37 | + left_join(current_tags, by="tag") %>% |
| 38 | + mutate(id = case_when(pit_tag==TRUE & is.na(id) ~ paste0(tag, "_", species, "_1") , |
| 39 | + TRUE ~ id)) |
| 40 | + |
| 41 | + new_data <- rbind(new_data1,new_data2) |
| 42 | + |
| 43 | + # get latest untagged id number |
| 44 | + max_id <- all_ids %>% filter(pit_tag==FALSE) %>% |
| 45 | + mutate(untag_id = sub("_.*", "", id)) %>% |
| 46 | + mutate(untag_id=as.numeric(untag_id)) %>% |
| 47 | + summarise(max(untag_id,na.rm=TRUE)) |
| 48 | + |
| 49 | + #assign numbers to untagged individuals |
| 50 | + unks <- which(is.na(new_data$species) == FALSE & new_data$pit_tag==FALSE) |
| 51 | + nunks <- length(unks) |
| 52 | + |
| 53 | + start_id <- max_id[1,1] + 1 |
| 54 | + end_id <- start_id + nunks - 1 |
| 55 | + new_data$id[unks] <- start_id:end_id |
| 56 | + |
| 57 | + # create new PIT tag based ids |
| 58 | + new_data <- new_data %>% |
| 59 | + mutate(id = case_when(pit_tag==FALSE & is.na(species)==FALSE ~ paste0(id, "_", species, "_1") , |
| 60 | + TRUE ~ id)) |
| 61 | + |
| 62 | + new_data %>% |
| 63 | + dplyr::mutate(id = ifelse(is.na(species), NA, id), |
| 64 | + pit_tag = ifelse(is.na(species), NA, pit_tag)) |
| 65 | + |
| 66 | + } |
| 67 | + |
1 | 68 | # |
2 | 69 | # determines if a tag is a PIT tag by its structure |
3 | 70 | # requires species input for comparison, returns a logical vector |
|
0 commit comments