I have a dataframe that generates from a folder that users will place several .csv files into. The .csv files will always have the same column structure, however they vary in row length. The idea is to make a single dataframe with all of the .csv files. When I use the code below with multiple .csv files I receive the following error message: "Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 88, 259"
temp <- list.files(pattern="*.csv", path = dir, full.names = TRUE)
importDM<-lapply(temp, read.csv, header = TRUE)
rawDM <- as.data.frame(importDM)
rawDM$Created.Date <- as.Date(rawDM$Created.Date...Time, format="%d/%m/%Y")
rawDM$Week <- strftime(rawDM$Created.Date,format="%W")
Something that will be an issue down the road as well is I want only the first .csv file added to be used for the header, as I believe with the code as it is will just lapply the header into the dataframe with each .csv file added.
Cheers,
importDM
is a list of data frames, you probably wantrawDM = do.call(rbind, importDM)
rather thanas.data.frame
. If you want the header the first one to be used, then before thedo.call
go through and set the names of each data frame to the names of the first one.for (i in 2:length(importDM)) importDM[[i]] = setNames(importDM[[i]], names(importDM[[1]]))
. - Gregor Thomas