0

I have a directory of 332 Excel files which I'm reading into R as follows:

list_files <- list.files(path = "C:/Users/mslomka/Documents/R/Datasets/Week 2", pattern = ".csv")
read_files <- lapply(list_files, read.csv)

However, when I subset the read_files list, all the components are lists rather than data frames/matrices (which is what they should be, given that they are Excel files).

I would like to either (a) read all the excels in as data frame, or (b) create a loop that coerces each of the lists into a data frame.

I have tried several approaches for (b) but none have worked, for example lapply(read_files, as.data.frame). I have only managed to coerce individual lists into data frames, for example by doing as.data.frame(read_files[1]).

Any help would be greatly appreciated!

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Slom
  • 1

2 Answers2

3

Some useful infos here + this meaningful picture from Twitter

enter image description here

Community
  • 1
  • 1
DianeBeldame
  • 156
  • 6
  • I get it, but that doesn't seem like the right container to begin with. Who puts pepper packets in a pepper shaker? – Rich Scriven Jun 03 '16 at 20:43
  • Like @pfabri mentionned it `do.call(rbind,read_files)` will gather pepper packets but you'll lose some kind of information. If data is splitted in several files, I suppose there's a good reason. Maybe the wrapping contains infos like "black pepper", "green pepper", "pink pepper"... Lists forces you to use `lapply` and functions – DianeBeldame Jun 09 '16 at 21:50
0

If I understand you correctly this gives you a list of lists:

read_files <- lapply(list_files, read.csv)

i.e. read.files[[i]] would return a list.

You have also managed to convert each of those [[i]] lists into dataframes by doing lapply(read_files, as.data.frame).

Do you now want to create one dataframe which contains all the information from each of the dataframes contained in read_files? If so, this will do the job:

df <- do.call(rbind, read_files)

Is this what you wanted?

pfabri
  • 885
  • 1
  • 9
  • 25