0
votes

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,

1
Since importDM is a list of data frames, you probably want rawDM = do.call(rbind, importDM) rather than as.data.frame. If you want the header the first one to be used, then before the do.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
Thanks Gregor for the speedy response! However it did not work for me! "Error in as.Date.default(rawDM$Created.Date...Time, format = "%d/%m/%Y") : do not know how to convert 'rawDM$Created.Date...Time' to class “Date”" Forgot to mention I have a date conversion code I have edited original post. - Nicholas Anderson

1 Answers

0
votes

Found an answer on a blog elsewhere, here was the final code:

temp <- list.files(pattern="*.csv", path = dir, full.names = TRUE)
importDM<-do.call("rbind", lapply(temp, read.csv, header = TRUE))
rawDM <- as.data.frame(importDM)