Data downloaded from the St. Louis Metropolitan Police Department are downloaded with incorrect file paths - e.g. January2008.CSV.html. This function iterates over all files in a given path and replaces their file extensions. Thus January2008.CSV.html will be replaced by january2008.csv. There should be no more than 12 files in a given path, and all should correspond to the same year.

cs_prep_year(path, verbose = FALSE)

Arguments

path

File path where raw STLMPD data are

verbose

If TRUE, returns a tibble with results; otherwise if FALSE, no output is returned.

Value

A tibble containing old file names and new file names for reference is verbose = TRUE. Otherwise, no output is returned. This function will change all problematic filenames in the specified path.

Examples

# create temporary directory tmpdir <- tempdir() fs::dir_create(paste0(tmpdir,"/data/")) # load sample files into temporary directory cs_example(path = paste0(tmpdir,"/data/")) # list files list.files(paste0(tmpdir,"/data/"))
#> [1] "April2017.CSV.html" "August2017.CSV.html" "December2017.CSV.html" #> [4] "February2017.CSV.html" "January2017.CSV.html" "July2017.CSV.html" #> [7] "June2017.CSV.html" "March2017.CSV.html" "May2017.CSV.html" #> [10] "November2017.CSV.html" "October2017.CSV.html" "September2017.CSV.html"
# prep sample files cs_prep_year(path = paste0(tmpdir,"/data/")) # list files again list.files(paste0(tmpdir,"/data/"))
#> [1] "april2017.csv" "august2017.csv" "december2017.csv" #> [4] "february2017.csv" "january2017.csv" "july2017.csv" #> [7] "june2017.csv" "march2017.csv" "may2017.csv" #> [10] "november2017.csv" "october2017.csv" "september2017.csv"
# delete data fs::dir_delete(paste0(tmpdir,"/data/")) # create temporary directory fs::dir_create(paste0(tmpdir,"/data/")) # load sample files into temporary directory cs_example(path = paste0(tmpdir,"/data/")) # prep sample files cs_prep_year(path = paste0(tmpdir,"/data/"), verbose = TRUE)
#> # A tibble: 12 x 2 #> original new #> <chr> <chr> #> 1 April2017.CSV.html april2017.csv #> 2 August2017.CSV.html august2017.csv #> 3 December2017.CSV.html december2017.csv #> 4 February2017.CSV.html february2017.csv #> 5 January2017.CSV.html january2017.csv #> 6 July2017.CSV.html july2017.csv #> 7 June2017.CSV.html june2017.csv #> 8 March2017.CSV.html march2017.csv #> 9 May2017.CSV.html may2017.csv #> 10 November2017.CSV.html november2017.csv #> 11 October2017.CSV.html october2017.csv #> 12 September2017.CSV.html september2017.csv
# delete data again fs::dir_delete(paste0(tmpdir,"/data/"))