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:
library(tidyverse)
# 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")
infiles
# 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
return(body)
}
# 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)
input.df