Part of Pedigree Curation
Usage
convertDate(ped, timeOrigin = as.Date("1970-01-01"), reportErrors = FALSE)Arguments
- ped
a dataframe of pedigree information that may contain birth, death, departure, or exit dates. The fields are optional, but will be used if present.(optional fields: birth, death, departure, and exit). If it has a
recordStatuscolumn, only the records marked"added"(animals added as stand-ins for unknown parents) are left unconverted. Every other record, including one whoserecordStatusisNAor not recognized, is a real animal and has its dates converted and checked.- timeOrigin
date object used by
as.Dateto setorigin.- reportErrors
logical value. If
FALSE(default), an invalid date stops with an error naming the column and the rows. IfTRUE, every date column is scanned and the converted pedigree is not returned; instead the result is a sorted character vector of the row numbers with an invalid date (a row is repeated once for each column in which it has one), orNULLwhen there are none. Row numbers, here and in the error message, are positions inpedas passed in, so records marked"added"count toward the numbering wherever they sit.
Value
A dataframe with an updated table with date columns converted from
character data type to Date data type. A date must be in the
form YYYY-MM-DD (month and day may have one digit), or
YYYYMMDD when no date in the column has a separator. A year before
1000 is invalid. A value that does not conform is an error (see
reportErrors), not set to NA. NA values and blank strings are left
as NA. A date column must be character, factor,
logical, integer or Date; any other class stops with
an error. Records marked "added" are not checked and are returned
after the other records. With reportErrors = TRUE see that
argument instead.
Examples
library(lubridate)
set_seed(10)
someBirthDates <- paste0(
sample(seq(0, 15, by = 3), 10,
replace = TRUE
) + 2000, "-",
sample(1:12, 10, replace = TRUE), "-",
sample(1:28, 10, replace = TRUE)
)
someBadBirthDates <- paste0(
sample(1:12, 10, replace = TRUE), "-",
sample(1:28, 10, replace = TRUE), "-",
sample(seq(0, 15, by = 3), 10,
replace = TRUE
) + 2000
)
someDeathDates <- sample(someBirthDates, length(someBirthDates),
replace = FALSE
)
someDepartureDates <- sample(someBirthDates, length(someBirthDates),
replace = FALSE
)
ped1 <- data.frame(
birth = someBadBirthDates, death = someDeathDates,
departure = someDepartureDates
)
someDates <- ymd(someBirthDates)
ped2 <- data.frame(
birth = someDates, death = someDeathDates,
departure = someDepartureDates
)
ped3 <- data.frame(
birth = someBirthDates, death = someDeathDates,
departure = someDepartureDates
)
someNADeathDates <- someDeathDates
someNADeathDates[c(1, 3, 5)] <- ""
someNABirthDates <- someDates
someNABirthDates[c(2, 4, 6)] <- NA
ped4 <- data.frame(
birth = someNABirthDates, death = someNADeathDates,
departure = someDepartureDates
)
## convertDate identifies bad dates
result <- tryCatch(
{
convertDate(ped1)
},
warning = function(w) {
print("Warning in date")
},
error = function(e) {
print("Error in date")
}
)
#> [1] "Error in date"
## convertDate with error flag returns error list and not an error
convertDate(ped1, reportErrors = TRUE)
#> [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
## convertDate recognizes good dates
all(is.Date(convertDate(ped2)$birth))
#> [1] TRUE
all(is.Date(convertDate(ped3)$birth))
#> [1] TRUE
## convertDate handles NA and empty character string values correctly
convertDate(ped4)
#> birth death departure
#> 1 2009-08-25 <NA> 2009-08-25
#> 2 <NA> 2003-07-18 2000-05-12
#> 3 2006-02-22 <NA> 2003-06-20
#> 4 <NA> 2006-02-22 2012-08-10
#> 5 2000-05-12 <NA> 2006-11-10
#> 6 <NA> 2003-04-07 2006-02-22
#> 7 2003-01-24 2003-01-24 2003-07-18
#> 8 2003-04-07 2009-05-22 2009-05-22
#> 9 2009-05-22 2006-11-10 2003-04-07
#> 10 2006-11-10 2000-05-12 2003-01-24
