Skip to contents

This function extracts 4-digit years (in the range 1900–2099) from a vector of strings.

Usage

extract_year(full_name)

Arguments

full_name

A character vector containing strings with embedded 4-digit years.

Value

An integer named vector of years, with names corresponding to the input strings. If no year is found in a string, the corresponding value will be `NA`.

Examples

extract_year(c(
  "A/H1N1/South Carolina/1/1918",
  "A/H3N2/Darwin/9/2021",
  "B/Sichuan/379/1999"
))
#> A/H1N1/South Carolina/1/1918         A/H3N2/Darwin/9/2021 
#>                         1918                         2021 
#>           B/Sichuan/379/1999 
#>                         1999 
seqs <- c(
  "A/H1N1/South Carolina/1/1918" = "mktiialsyifclvlgqdfpgndnstatlclgh",
  "A/H3N2/Darwin/9/2021" = "mktiialsnilclvfaqkipgndnstat",
  "B/Sichuan/379/1999" = "drictgitssnsphvvktatqgevnvtgai"
)
extract_year(names(seqs))
#> A/H1N1/South Carolina/1/1918         A/H3N2/Darwin/9/2021 
#>                         1918                         2021 
#>           B/Sichuan/379/1999 
#>                         1999