I have multiple csv files that I want to import into R.
How do I read multiple files into R and add a column with the date text from the file name in the first column?
Need here:
For each csv file take the date found in the file name and add it as the first column of each dataframe before rbinding them.
Here is the code without the date column from the file name:
all <- lapply(c("file_10-16-2017.csv",
"file_10-17-2017.csv",
"file_10-18-2017.csv",
function(x){
read_csv(x, skip = 10)}) %>% bind_rows()
I want my final result to look something like this:
Date_Pulled Week Date spot1 Site ID test
10-16-2017 10/15/17 10/16/2017 trial trial134
. . . . .
. . . . .
. . . . .
10-17-2017 10/15/17 10/16/2017 trial trial134
. . . . .
. . . . .
. . . . .
Any help would be great, thanks!
I was able to come up with another method:
filenames <- list.files(path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE)
read_csv_filename <- function(filenames){
ret <- read.csv(filenames, skip = 10)
ret$Source <- filenames #EDIT
ret
}
import.list <- ldply(filenames, read_csv_filename)
This should do the trick