bodega18
bodega18

Reputation: 654

Extract Part of File Name and Mutate into Column dplyr R

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

Answers (1)

Ronak Shah
Ronak Shah

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

Related Questions