Skip to content

Commit

Permalink
Fixed infrequent, prep for CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
rosieluain committed Sep 1, 2023
1 parent 17a8f71 commit 60c572c
Show file tree
Hide file tree
Showing 43 changed files with 3,733 additions and 3,371 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
^pkgdown$
^\.github$
^working$
^cran-comments\.md$
^doc$
^Meta$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
working/
pkgdown/
inst/doc
/doc/
/Meta/
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: mort
Title: mort: Identifying potential mortalities and expelled tags in aquatic acoustic telemetry arrays
Title: Identifying Potential Mortalities and Expelled Tags in Aquatic Acoustic Telemetry Arrays
Version: 0.0.1
Authors@R: c(
person("Rosie", "Smith", , "[email protected]", role = c("aut", "cre"),
person("Rosie", "Smith", , "[email protected]", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0003-2675-4452")),
person("Heidi", "Swanson", role = "fnd")
)
Expand All @@ -24,7 +24,9 @@ Suggests:
ggplot2,
knitr,
plotly,
rmarkdown
rmarkdown,
testthat (>= 3.0.0)
VignetteBuilder: knitr
Depends:
R (>= 2.10)
R (>= 3.5.0)
Config/testthat/edition: 3
93 changes: 92 additions & 1 deletion R/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ extractres<-function(data,type){
}



#' Compare specified to default units
#' @description Compare specified units to package default units and issue
#' a warning if there is no match.
Expand Down Expand Up @@ -204,3 +203,95 @@ unitcheck<-function(type,units,data){
}
}


#' Convert units of time
#' @description Convert a period of time into different units.
#'
#' @param unit1 Initial units of the period of time.
#' @param unit2 Desired units of the period of time.
#' @param val1 Period of time, in the units of `unit1`
#'
#' @return A numeric value of the period time in the desired units (`unit2`).
#' @keywords internal
#' @noRd
#'
#' @examples
#' unitconvert("secs","mins",60)
#' unitconvert("weeks","hours",1)
unitconvert<-function(unit1,unit2,val1){
if (unit1=="secs"){
if (unit2=="mins"){
val.new<-val1/60
}
if (unit2=="hours"){
val.new<-val1/60/60
}
if (unit2=="days"){
val.new<-val1/60/60/24
}
if (unit2=="weeks"){
val.new<-val1/60/60/24/7
}
}
else if (unit1=="mins"){
if (unit2=="secs"){
val.new<-val1*60
}
if (unit2=="hours"){
val.new<-val1/60
}
if (unit2=="days"){
val.new<-val1/60/24
}
if (unit2=="weeks"){
val.new<-val1/60/24/7
}
}
else if (unit1=="hours"){
if (unit2=="secs"){
val.new<-val1*60*60
}
if (unit2=="mins"){
val.new<-val1*60
}
if (unit2=="days"){
val.new<-val1/24
}
if (unit2=="weeks"){
val.new<-val1/24/7
}
}
else if (unit1=="days"){
if (unit2=="secs"){
val.new<-val1*60*60*24
}
if (unit2=="mins"){
val.new<-val1*60*24
}
if (unit2=="hours"){
val.new<-val1*24
}
if (unit2=="weeks"){
val.new<-val1/7
}
}
else if (unit1=="weeks"){
if (unit2=="secs"){
val.new<-val1*60*60*24*7
}
if (unit2=="mins"){
val.new<-val1*60*24*7
}
if (unit2=="hours"){
val.new<-val1*24*7
}
if (unit2=="days"){
val.new<-val1*7
}
}
else{
stop("Units are not supported. Units must be 'secs', 'mins', 'hours', 'days', or 'weeks'.")
}

val.new
}
29 changes: 19 additions & 10 deletions R/mort.R
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,16 @@ morts<-function(data,type="mort",ID,station,res.start="auto",res.end="auto",
#' ## Recent example
#' inf_recent<-infrequent(data=events,type="mort",ID="ID",
#' station="Station.Name",method="recent",
#' threshold=0.5,threshold.units="hours",
#' threshold=72,threshold.units="hours",
#' recent.period=52,recent.units="weeks",
#' progress.bar=FALSE)
#' head(inf_recent)
#'
#' ## User-defined example
#' inf_defined<-infrequent(data=events,type="mort",ID="ID",
#' station="Station.Name",method="defined",
#' threshold=0.5,threshold.units="hours",
#' start="2019-10-01",end="2020-06-01",
#' threshold=12,threshold.units="hours",
#' start="2006-06-15",end="2006-10-15",
#' progress.bar=FALSE)
#' head(inf_defined)
infrequent<-function(data,type="mort",ID,station,res.start="auto",
Expand Down Expand Up @@ -699,6 +699,10 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
if (is.null(recent.units)){
stop("recent.units is not specified")
}
# Convert threshold units if threshold units and residence units do not match
if (units!=threshold.units){
threshold<-unitconvert(unit1=threshold.units,unit2=units,val1=threshold)
}
print("Identifying mortalities from infrequent detections")
if (progress.bar==TRUE){
pb<-txtProgressBar(1,length(tag),style=3)
Expand All @@ -713,28 +717,29 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
j<-min(which(difftime(res.temp[[res.end]][nrow(res.temp)],
res.temp[[res.end]],
units=recent.units)<=recent.period))
res.temp<-res.temp[j:nrow(res.temp),]
if (is.null(ddd)){
if (nrow(res.temp)>=1&
length(unique(res.temp[[station]][j:nrow(res.temp)]))==1&
length(unique(res.temp[[station]]))==1&
sum(res.temp[[residences]])<threshold){
if (tag[i] %in% inf.morts[[ID]]){
j<-which(inf.morts[[ID]]==tag[i])
if (res.temp[[res.start]][1]<inf.morts[[res.start]][j]){
inf.morts[j,]<-res.temp[1,]
k<-which(inf.morts[[ID]]==tag[i])
if (res.temp[[res.start]][1]<inf.morts[[res.start]][k]){
inf.morts[k,]<-res.temp[1,]
if (!is.null(morts.prev)&backwards==TRUE){
new.morts<-c(new.morts,j)
new.morts<-c(new.morts,k)
}
}
}
else {
inf.morts[nrow(inf.morts)+1,]<-res.temp[j,]
inf.morts[nrow(inf.morts)+1,]<-res.temp[1,]
}
}
}
else {
if (nrow(res.temp)>0&
sum(res.temp[[residences]])<threshold){
res.temp.drift<-drift(data=res.temp[j:nrow(res.temp),],
res.temp.drift<-drift(data=res.temp,
type=type,ID=ID,station=station,
res.start=res.start,res.end=res.end,
residences=residences,units=units,ddd=ddd,
Expand Down Expand Up @@ -857,6 +862,10 @@ infrequent<-function(data,type="mort",ID,station,res.start="auto",
if (!is(end,"POSIXt")){
stop("end is not in the format YYYY-mm-dd HH:MM:SS")
}
# Convert threshold units if threshold units and residence units do not match
if (units!=threshold.units){
threshold<-unitconvert(unit1=threshold.units,unit2=units,val1=threshold)
}
if (progress.bar==TRUE){
pb<-txtProgressBar(1,length(tag),style=3)
}
Expand Down
Loading

0 comments on commit 60c572c

Please sign in to comment.