I'd use purrr for this:
Here's an example (note that I've put my three files in a folder in My Documents called "csv-files").
library(tidyverse)
# see the files
read_csv("~/csv-files/file1.csv", show_col_types = F)
#> # A tibble: 5 x 2
#> x y
#> <dbl> <dbl>
#> 1 1 5
#> 2 2 10
#> 3 3 15
#> 4 4 20
#> 5 5 25
read_csv("~/csv-files/file2.csv", show_col_types = F)
#> # A tibble: 5 x 2
#> x y
#> <dbl> <dbl>
#> 1 1 2
#> 2 2 4
#> 3 3 6
#> 4 4 8
#> 5 5 10
read_csv("~/csv-files/file3.csv", show_col_types = F)
#> # A tibble: 5 x 2
#> x y
#> <dbl> <dbl>
#> 1 1 3
#> 2 2 6
#> 3 3 9
#> 4 4 12
#> 5 5 15
# function to summarise file
sum_file = function(path){
dat = read_csv(path, show_col_types = F)
tibble(file = path,
sum = sum(dat$y, na.rm = T))
}
# list files
files = list.files("csv-files", pattern = ".csv", full.names = T)
# summarise all
summary = map_dfr(files, sum_file)
summary
#> # A tibble: 3 x 2
#> file sum
#> <chr> <dbl>
#> 1 csv-files/file1.csv 75
#> 2 csv-files/file2.csv 30
#> 3 csv-files/file3.csv 45
write_csv(summary, "~/summary_file.csv")
Created on 2022-01-07 by the reprex package (v2.0.1)