read csv files all at once

I have a strange pb reading many csv files on R. I cannot import all at once because the files have this structure in the unique column :

Source: Month 195911
Method used: Weight
Zone: Europe
=> Total,"354","8667","2118"

I want to keep only the title "Month 195911" as the column name and also all the lines on area (Area1...). My code is:

my_data <- import_list(dir("C:/Users/", pattern = ".csv"), rbind = TRUE)

My question is how to read all at once while holiding title line (1) and deleting useless lines(2-5) and import the remaining lines of all files? Many thanks.

Hi @supermarco
I'm not sure if you have solved this problem, but in case you haven't, here is some code you can try:


# Get the list of CSV files to read
# Assumes all required files are in the current working directory with no others 
infiles <- list.files(pattern=".csv")

# Make a user-defined function to read one file
my_reader <- function(infile){
  # Read and edit line 1 from the file
  fileID <- readLines(con=infile, n=1)
  fileID <- str_split_i(fileID, ": Month ", 2)
  fileID <- str_remove(fileID, ",,,")  # Extra characters because I made CSV files
  print(fileID) # to show progress
  # Read the body of the data, fix column names, add fileID column
  body <- read.csv(infile, header=FALSE, skip=5)
  names(body) <- c("Area","Total1","Total2","Total3")
  body$fileID <- fileID

# Read all the files into a list
input.lst <- lapply(infiles, my_reader)

# bind all the list parts into a data.frame
input.df <- bind_rows(input.lst)