Reputation: 375
I am trying to save multiple csv files in one df and include a new column with the date of the file in the df. I already read all the files to get one df but I can't add the date column per file. Im using the next code
ccn_files <- list.files(pattern = '*.csv', path = "input/CCN/") ##Creates a list of all the files
ccn_data_raw <- do.call("rbind", ##Apply the bind to the files
lapply(ccn_files, ##call the list
function(x) ##apply the next function
read.csv(paste("input/CCN/", x, sep=''),fill = T, header = TRUE,
skip = 4)))
I was also able to get the date from all the files in a vector using this line
test <- ymd(substr(ccn_files,14,19))
How can I add this line inside the first chunk of code so it does what I want?
Upvotes: 0
Views: 140
Reputation: 389055
We can use Map
ccn_data_raw <- do.call(rbind, Map(cbind, lapply(ccn_files,
function(x) read.csv(paste("input/CCN/", x, sep=''),fill = TRUE,
header = TRUE, skip = 4)), date = test))
Or using purrr
functions :
library(purrr)
ccn_data_raw <- map2_df(map(ccn_files, function(x)
read.csv(paste("input/CCN/", x, sep=''), fill = TRUE, header = TRUE,
skip = 4)), test, cbind)
Upvotes: 1