rexceldataframexlconnectreadxl

Import excel sheets as individual dataframes into R


I have an excel file with 48 sheets, I used this code to read multiple sheets:

lst <- lapply(1:48, function(i) read_excel("my_file.xlsx", sheet = i))

lst has the information, but I would like to create individual dataframes in R. So, I would like to create 48 tables. How can I go about this?


Solution

  • We can use readxl package:

    library(readxl)
    
    my_sheet_names <- excel_sheets("my_file.xlsx")
    my_sheets <- lapply(my_sheet_names, function(x) read_excel("my_file.xlsx", sheet = x))
    names(my_sheets) <- my_sheet_names
    

    This will give you a list of dataframes, each will be one of your sheets. You can then save them as individual dataframes if desired:

    list2env(my_sheets, envir=.GlobalEnv)