getYear: Get Year From Date

getYearR Documentation

Get Year From Date

Description

This function extracts the year from dates associated with the collection or identification of biological records.

Usage

getYear(x, noYear = "s.d.")

Arguments

x

the character string or vector containing the dates.

noYear

character. Standard notation for missing date information. Default to "n.d.".

Details

The function extracts the year from a given date in various different formats, but not all of them (see examples below). So, please consider putting your vector of dates in a standardized format prior to the year extraction (see note below).

For some types of format, the function assumes internally that years are given as numbers with 4 digits or with 2 digits but higher than 31. If the year is not given in one of these formats, the function returns the date as it is provided.

Value

The year contained in the character string x.

Note

If your dates are all stored in some specific format, you can convert them to the ISO 8601 format (YYYY-MM-DD, e.g. 1981-03-25), which is the standard format suggested by the Biodiversity Information Standards (TDWG). To do so you have to provide your vector of dates and its format to the as.Date function from the base R package. Some examples:

  1. The date format in this case is '%d/%m/%y', i.e. day, month, two-digits year, separated by slashs:

dates <- c("25/03/81", "05/12/81") as.Date(dates, format = "%d/%m/%y")

  1. Date format is "%d/%b/%Y", i.e. day, month, four-digits year, separated by slashs:

dates <- c("25/03/1981", "05/12/1981") as.Date(dates, format = "%d/%m/%Y")

  1. Date format is '%d %b %y', i.e. day, abbreviated month, four-digits year, separated by spaces:

dates <- c("25 Mar 1981", "05 Dec 1981") as.Date(dates, format = "%d %b %Y")

Author(s)

Renato A. F. de Lima

References

Conn, Barry J. (ed.) (1996). HISPID 3 - Herbarium Information Standards and Protocols for Interchange of Data. Herbarium Information Systems Committee' (HISCOM). https://www.tdwg.org/standards/hispid3/

Willemse, L.P., van Welzen, P.C. & Mols, J.B. (2008). Standardisation in data-entry across databases: Avoiding Babylonian confusion. Taxon 57(2): 343-345.

Examples

# A vector with some typical examples of formats found in herbarium labels
dates <- c("25/03/1981","25-03-1981","03/1981","03-1981","1981","1.981","1,981",
         "25/III/1981","25-III-1981","III/1981","III-1981","25031981","25 03 1981","'81",
         "March 1981","Mar. 1981","Mar 1981","25 Mar 1981","n.d.","s.d.","s/d","",NA,
         "1981-03-25", "81/01/25", "25/03/81", "21/12/21", "21-12-81", "Mar. 25-'81")

# Using the function to extract the year
getYear(dates)

# Using the function with a different character to indicate missing dates
getYear(dates, noYear = "n.d.")

# Currently, the function does not work for (needs checking):
dates <- c("31.12.1982","1982.31.12", '31/12')
getYear(dates, noYear = 'pouette')


LimaRAF/plantR documentation built on Jan. 1, 2023, 10:18 a.m.