It's a problem of misplaced parentheses. Your original code from inside the for loop, with helpful whitespace added:
assign(
paste0(
gsub("\\..*", "", file_list[i]),
read.csv(file_list[i])
) %>%
mutate(
Report.Date = as.Date(gsub("\\D", "", file_list[i]), format = "%Y%m%d")
)
)
The value returned by paste0(), which is a character vector, is being passed to mutate(). I suggest splitting up this single statement. One possibility:
# Using seq_along() to avoid 1:0 problems
for (i in seq_along(file_list)) {
# More explicit regex to avoid problems if reports have digits in the name
date_stamp <- gsub(".*(\\d{4}-\\d{2}-\\d{2})\\.csv$", "\\1", file_list[i])
report_df <- read.csv(file_list[i])
report_df[["Report.Date"]] <- as.Date(date_stamp, format = "%Y-%m-%d")
df_name <- tools::file_path_sans_ext(file_list[i])
assign(df_name, report_df)
}
I'd also recommend @hinkelman's advice:
But go further to suggest wrapping the reading logic in a function and using lapply:
read_report_data <- function(path) {
date_stamp <- gsub(".*(\\d{4}-\\d{2}-\\d{2})\\.csv$", "\\1", path)
report_df <- read.csv(path)
report_df[["Report.Date"]] <- as.Date(date_stamp, format = "%Y-%m-%d")
report_df
}
reports <- lapply(file_list, read_report_data)
names(reports) <- tools::file_path_sans_ext(file_list)