age_calc update to handle vector of enddates 2021-11-08 11:00:30

This commit is contained in:
AG Damsbo 2021-11-08 11:00:30 +01:00
parent 21391d9392
commit bb0d627259
4 changed files with 22 additions and 5 deletions

View File

@ -1,6 +1,6 @@
Package: daDoctoR Package: daDoctoR
Title: Functions For Health Research Title: Functions For Health Research
Version: 0.21.16 Version: 0.21.17
Year: 2021 Year: 2021
Author: Andreas Gammelgaard Damsbo <agdamsbo@pm.me> Author: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me> Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
@ -10,4 +10,4 @@ Suggest: shiny
License: GPL (>= 2) License: GPL (>= 2)
Encoding: UTF-8 Encoding: UTF-8
LazyData: true LazyData: true
RoxygenNote: 7.1.1 RoxygenNote: 7.1.2

View File

@ -9,7 +9,7 @@
#' @export #' @export
#' @examples #' @examples
#' ##Kim Larsen (cpr is known from album) #' ##Kim Larsen (cpr is known from album)
#' dob<-dob_extract_cpr("231045-0637") #' dob<-daDoctoR::dob_extract_cpr("231045-0637")
#' date<-as.Date("2018-09-30") #' date<-as.Date("2018-09-30")
#' trunc(age_calc(dob,date)) #' trunc(age_calc(dob,date))
@ -19,9 +19,15 @@ age_calc<-function (dob, enddate = Sys.Date(), units = "years", precise = TRUE)
if (!inherits(dob, "Date") | !inherits(enddate, "Date")) { if (!inherits(dob, "Date") | !inherits(enddate, "Date")) {
stop("Both dob and enddate must be Date class objects") stop("Both dob and enddate must be Date class objects")
} }
if (enddate < dob) {
if (length(dob)==1 && enddate < dob) {
stop("End date must be a date after date of birth") stop("End date must be a date after date of birth")
} }
if (length(dob)>1 && any(enddate < dob)) {
stop("End date must be a date after date of birth")
}
start <- as.POSIXlt(dob) start <- as.POSIXlt(dob)
end <- as.POSIXlt(enddate) end <- as.POSIXlt(enddate)
if (precise) { if (precise) {
@ -74,6 +80,7 @@ age_calc<-function (dob, enddate = Sys.Date(), units = "years", precise = TRUE)
result <- years result <- years
} }
} }
else { else {
stop("Unrecognized units. Please choose years, months, or days.") stop("Unrecognized units. Please choose years, months, or days.")
} }

View File

@ -20,7 +20,7 @@ For age calculations.
} }
\examples{ \examples{
##Kim Larsen (cpr is known from album) ##Kim Larsen (cpr is known from album)
dob<-dob_extract_cpr("231045-0637") dob<-daDoctoR::dob_extract_cpr("231045-0637")
date<-as.Date("2018-09-30") date<-as.Date("2018-09-30")
trunc(age_calc(dob,date)) trunc(age_calc(dob,date))
} }

View File

@ -9,3 +9,13 @@ devtools::document()
# Inspiration: "https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/" # Inspiration: "https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/"
# Author@R: person("Andreas", "Gammelgaard Damsbo", email = "agdamsbo@pm.me", role = c("cre", "aut")) # Author@R: person("Andreas", "Gammelgaard Damsbo", email = "agdamsbo@pm.me", role = c("cre", "aut"))
# Commit and push
commit_message<-"age_calc update to handle vector of enddates"
library(git2r)
library(lubridate)
git2r::commit(all=TRUE, message=paste(commit_message,now()))
system("/usr/bin/git push origin HEAD:refs/heads/main")