extract_datetime: Extract and parse date-times within strings such as file...

Description Usage Arguments Value Examples

View source: R/extract_datetime.R

Description

Extract and parse date-times within strings such as filenames. Currently only handles format "yyyy?mm?dd?HH?MM?(SS)", where "?" represents an optional space or punctuation character, and the seconds slot (SS) is optional.

Usage

1
2
3
4
5
6
7
extract_datetime(
  x,
  remove_path = TRUE,
  year_min = 2000,
  year_max = as.integer(format(Sys.Date(), "%Y")),
  if_multiple = "use_last"
)

Arguments

x

Vector of strings such as file names

remove_path

Logical indicating whether to strip file paths from x using basename (up to and including the last path separator) prior to searching for date values. Defaults to TRUE.

year_min

Minimum year value expected. Targeting an expected year range helps avoid false positive matches to number sequences that are not dates. Defaults to 2000.

year_max

Maximum year value expected. Defaults to the current year based on Sys.date.

if_multiple

If multiple date-times within a string, return only the first ("use_first"), or only the last ("use_last"). Defaults to "use_last".

Value

Vector of date-times (of class POSIXct/POSIXt). Returns NA when no date-time found within a given string.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
files <- c(
  "~/Documents/2020-02-01/myfile-2020-06-30-1230.csv",
  "2020_06_30_052051_Database_Complete_v1230.csv",
  "~/Desktop/data__cleaned1.xlsx",
  "~/Desktop/data__cleaned__2020-07-01_16-25.xlsx",
  "22062020-covid19-cases.xlsm",
  "/Documents/2015/PhD.Data.20091020_1247.Final.xls",
  "/exports/2015-03-05/export-2015-03-05_1352.xls"
)

extract_datetime(files)

epicentre-msf/llutils documentation built on Nov. 9, 2020, 8:24 p.m.