@@ -15,7 +15,8 @@ update_portal_plots <- function() {
1515 # load plot data
1616 portal_plots = read.csv(" SiteandMethods/Portal_plots.csv" ,stringsAsFactors = FALSE )
1717 # load rodent data
18- rodentdat = read.csv(" Rodents/Portal_rodent.csv" ,stringsAsFactors = FALSE ,as.is = TRUE ,na.strings = ' ' )
18+ rodentdat = read.csv(" Rodents/Portal_rodent.csv" ,
19+ stringsAsFactors = FALSE ,as.is = TRUE ,na.strings = ' ' )
1920
2021 # define current plot treatments
2122
@@ -24,10 +25,23 @@ update_portal_plots <- function() {
2425 exclosures = c(2 ,3 ,8 ,15 ,19 ,20 ,21 ,22 )
2526
2627 # proceed only if rodentdat has more recent data than plot data
27- # find new rows
28- newrows = which(paste(rodentdat $ year ,rodentdat $ month ) %in% paste(portal_plots $ year ,portal_plots $ month )
28+ # find new rows
29+ newrows = which(paste(rodentdat $ year ,rodentdat $ month ) %in%
30+ paste(portal_plots $ year ,portal_plots $ month )
2931 == FALSE )
30- newdat = unique(na.omit(rodentdat [newrows ,c(2 ,4 )]))
32+
33+ if (length(newrows )> 0 ) {
34+
35+ latest <- unique(na.omit(rodentdat [newrows ,c(2 ,4 )])) %> %
36+ mutate(date = as.Date(paste(year ,month ," 1" ,sep = " -" )))
37+ current <- tail(portal_plots [,c(1 ,2 )],1 ) %> %
38+ mutate(date = as.Date(paste(year ,month ," 1" ,sep = " -" )))
39+ newdat <- data.frame (date = as.Date(current $ date : max(latest $ date ))) %> %
40+ mutate(year = lubridate :: year(date ),
41+ month = lubridate :: month(date )) %> %
42+ select(- date ) %> %
43+ unique() %> %
44+ anti_join(current [,1 : 2 ], by = join_by(year , month ))
3145
3246 if (nrow(newdat )> 0 ) {
3347
@@ -36,13 +50,14 @@ update_portal_plots <- function() {
3650
3751 newplots $ treatment = NA
3852
39- newplots = newplots %> % mutate( treatment = ifelse(( plot %in% removals ),
40- " removal" , treatment )) %> %
53+ newplots = newplots %> %
54+ mutate( treatment = ifelse(( plot %in% removals ), " removal" , treatment )) %> %
4155 mutate(treatment = ifelse((plot %in% exclosures )," exclosure" , treatment )) %> %
4256 mutate(treatment = ifelse((plot %in% controls )," control" , treatment ))
4357
44- portal_plots = bind_rows(portal_plots ,newplots )
45- }
58+ portal_plots = bind_rows(portal_plots ,newplots ) %> % arrange(year ,month ,plot )
59+
60+ }}
4661
4762 return (portal_plots )
4863}
0 commit comments