0

Hello I am attempting to create a single dataframe with multiple .csv files from a folder that will be updating/added to.

I have found previous answers on here however I am having a rather simple error

Error in read.table(file = file, header = header, sep = sep, quote = quote, : 'file' must be a character string or connection

dir<-"asdfasdfasdfasf/asdfasdfs" #change this to your directory

temp <- list.files(pattern="*.csv")
importDM<-lapply(temp, read.csv)

rawDM<-read.csv(importDM, header = TRUE)      #will read csv

Please let me know what I am doing wrong!

Cheers,

lmo
  • 37,904
  • 9
  • 56
  • 69
  • 1
    `do.call(rbind, lapply(temp, read.csv, header = TRUE))` should work. `importDM` is a list, not a character string; that's why the last line is giving you an error. – Weihuang Wong Aug 30 '16 at 04:57

3 Answers3

1

From what I see in the call to the list.files function you should add the path parameter and assign "dir" to it. The variable importDM is a list and the rawDM variable is not necessary since you called read.csv in lapply already. If the files have the same sructure of data you can convert importDM to a data frame with as.data.frame. If the list elements don't follow the same structure that it is a bit more work.

This is a working example whit three csv files.

    dir <-"./data" #change this to your directory
    temp <- list.files(pattern="*.csv", path = dir, full.names = TRUE)
    importDM<-lapply(temp, read.csv, header = FALSE)
    df <- as.data.frame(importDM) 

Each csv file contains three numbers (1,2,3 - 4,5,6 - 7,8,9)

Valter Beaković
  • 3,140
  • 2
  • 20
  • 30
0

Try this one:

temp <- list.files(pattern="*.csv")

dataset <- do.call(rbind,lapply(temp,
read.csv,header=TRUE))
MFR
  • 2,049
  • 3
  • 29
  • 53
0

This should do it -

dir<-"asdfasdfasdfasf/asdfasdfs" #change this to your directory
temp <- list.files(pattern="*.csv")
importDM<-lapply(temp, read.csv)
rawDM <- do.call(rbind, lapply(raw, read.csv, header = TRUE))
prateek1592
  • 547
  • 5
  • 13