0

I'm new to R and am struggling to create a list of dataframes from imported Excel files.

I have a list of xls files I have imported into R using readxl.

First I got the paths of the xls files in the working directory:

filenames<- list.files(pattern=".xls")

Then I read in the xls files using lapply to create the list called "data"

data<-lapply(filenames, function(x) read_excel(path=x,sheet=1))

I had hoped each element of data would already be a dataframe as the read_excel function help seems to indicate but class(data[i]) indicates they are all individual lists (not sure why-- maybe due to use of lapply?).

I am now trying to convert each element (the data from each xls file) to a dataframe by using either lapply with as.data.frame or by using a for loop. Neither approach is working for me.

With the for loop the data frames are not coming out correctly:

data.df=NULL

for (i in 1:length(data)) {
  data.df[i] = as.data.frame(data[i])
}

Another attempt with lapply that also does not result in a list of dataframes:

data.df<-lapply(data, function(x) as.data.frame(x))

What am I doing wrong?

user13999
  • 145
  • 6
  • try `data<-lapply(filenames, function(x) data.frame(read_excel(path=x,sheet=1)))`? – user2133017 Feb 19 '16 at 02:50
  • Thanks for the suggestion. Just tried it and it made a list of lists (not a list of dataframes). I want to be apply to get the column information from each excel sheet by addressing it like data[1]$col1 or data[2]$col1 etc. – user13999 Feb 19 '16 at 02:57
  • 2
    Not an exact duplicate, but the underlying issue is the use of `[` not `[[` to subset the list so the answer is in the linked question. – mnel Feb 19 '16 at 03:35

0 Answers0