Skip to content

Commit 973ec20

Browse files
committed
Update unique rodent ids [minor]
1 parent c281444 commit 973ec20

File tree

3 files changed

+8769
-8685
lines changed

3 files changed

+8769
-8685
lines changed

DataCleaningScripts/clean_pit_tags.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,70 @@
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+
168
#
269
# determines if a tag is a PIT tag by its structure
370
# requires species input for comparison, returns a logical vector

DataCleaningScripts/new_rodent_data.r

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,23 +365,40 @@ if (length(tags) > 0) {
365365
}
366366

367367
# Add unique tag ID column
368+
ws_tags <- add_id(ws, olddat, new_period)
368369

369-
#ws_tags <- clean_tags(ws, clean = TRUE, quiet = FALSE)
370+
old_ids <- olddat %>%
371+
filter(period<new_period-36) %>%
372+
select(species,tag,pit_tag,id) %>%
373+
distinct()
374+
old_tags <- old_ids %>%
375+
filter(pit_tag==TRUE) %>%
376+
select(species,tag,pit_tag,id) %>%
377+
distinct()
370378

371-
#if(is.na(ws_tags$id[which(!is.na(ws_tags$species))])) {
372-
# print("Duplicate individuals in tag data, recheck tags") }
373-
#if(dim(ws_tags)!=dim(ws)) {
374-
# print("Records dropped in tag data, recheck tags") }
379+
if(any(ws_tags$id[!is.na(ws_tags$id)] %in% old_ids$id)) {
380+
print("Duplicate ids created, check ids") }
381+
382+
if(any(ws_tags$tag %in% old_tags$tag)) {
383+
print("Duplicate PIT tags, recheck tags") }
384+
385+
if(any(is.na(ws_tags$id[which(!is.na(ws_tags$species))]))) {
386+
print("Duplicate individuals in tag data, recheck tags") }
387+
388+
if(dim(ws_tags)[1]>dim(ws)[1]) {
389+
print("Records duplicated, this tag has 2 IDs")
390+
print(ws_tags[duplicated(ws_tags[,-c(30:31)]),]) }
391+
392+
if(dim(ws_tags)[1]<dim(ws)[1]) {
393+
print("Records dropped in tag data, check tags") }
375394

376-
#ws <- dplyr::left_join(ws, ws_tags)
377-
ws<-dplyr::mutate(ws, pit_tag=NA, id=NA)
378395

379396
##############################################################################
380397
# 4. Append new data
381398
##############################################################################
382399

383400
# make column of record IDs for new data
384-
newdat = cbind(recordID = seq(max(olddat$recordID) + 1, max(olddat$recordID) + length(ws$month)), ws)
401+
newdat = cbind(recordID = seq(max(olddat$recordID) + 1, max(olddat$recordID) + length(ws$month)), ws_tags)
385402

386403
# append to existing data file
387404
#write.table(newdat, "./Rodents/Portal_rodent.csv", row.names = F, na = "", append=T, sep=",", col.names = F, quote = c(9,10,11,12,13,14,15,16,17,20,21,22,23,24,25,26,27,28,29))

0 commit comments

Comments
 (0)