user1471980 user1471980 - 7 days ago 6
R Question

how do you extract date from the file name in R?

I have to read files in a directory and extract the dates from the file names.

filenames <- list.files(path="C:/Downloads/vmstat", pattern="*vmstat*", full.names=TRUE)


filenames like this:

C:/Downloads/vmstat/vmstat.2016.11.22.Tue


I need to extract
2016.11.22
from this filename and assign it to a variable like
dat
. How could I extract the date from the filenames?

Answer

We can use gsub to match the substring that are not date and replace it with blanks ("")

gsub("^[^0-9]+\\.|\\.[A-Za-z]+$", "", filenames)
#[1] "2016.11.22"

Or extract the basename and then convert to Date class with lubridate

lubridate::ymd(basename(filenames))
#[1] "2016-11-22"

data

filenames <- "C:/Downloads/vmstat/vmstat.2016.11.22.Tue"