Reputation: 654
I have an excel file called "data_12.18.2020.xlsx". I want to extract the date part of the file name and generate a date column as I read in the file. Is there a way to do this within a dplyr call?
Upvotes: 0
Views: 410
Reputation: 389135
You can extract the date from the filename using regular expression and add it as new column using mutate
.
library(dplyr)
file <- "data_12.18.2020.xlsx"
date <- sub('data_(\\d+\\.\\d+.\\d+)\\.xlsx', '\\1', file)
df <- read.xlsx(file) %>% mutate(date = date)
Upvotes: 1