R - Excel Files
Modern R and Excel Files
Section titled “Modern R and Excel Files”Microsoft Excel (.xls, .xlsx) is a ubiquitous spreadsheet program. Interacting with Excel files is a common task in data analysis. While there are several packages to connect R and Excel, the modern standard is to use the readxl and writexl packages.
Why readxl and writexl?
Older packages like xlsx or gdata often relied on Java or Perl, which could be slow and difficult to set up. The readxl and writexl packages are:
- Fast: They are written in C/C++ for high performance.
- Dependency-Free: They have no external dependencies like Java, making installation and use seamless on any system.
- Tidyverse-Friendly:
readxlis part of the coretidyverseand plays nicely with other packages likedplyrandggplot2.
Step 1: Install the Required Packages
Section titled “Step 1: Install the Required Packages”You can install both packages from CRAN using the following command in the R console.
install.packages(c("readxl", "writexl"))Step 2: Prepare the Excel Input File
Section titled “Step 2: Prepare the Excel Input File”Create an Excel file named input.xlsx. In the first worksheet (which is usually named Sheet1 by default), enter the following employee data:
id name salary start_date dept1 Rick 623.3 2012-01-01 IT2 Dan 515.2 2013-09-23 Operations3 Michelle 611 2014-11-15 IT4 Ryan 729 2014-05-11 HR5 Gary 843.25 2015-03-27 Finance6 Nina 578 2013-05-21 IT7 Simon 632.8 2013-07-30 Operations8 Guru 722.5 2014-06-17 FinanceNext, create a second worksheet and name it cities. Enter the following location data:
name cityRick SeattleDan TampaMichelle ChicagoRyan SeattleGary HoustonNina BostonSimon MumbaiGuru DallasSave the file input.xlsx in your R project’s working directory.
Step 3: Reading Data from the Excel File
Section titled “Step 3: Reading Data from the Excel File”We use the read_excel() function from the readxl package. The result is imported as a tibble, a modern data frame.
# Load the librarylibrary(readxl)
# Read the first worksheet by its index (1-based)# read_excel() is smart enough to find the first sheet automatically if not specified.employee_data <- read_excel("input.xlsx", sheet = 1)print(employee_data)
# Read the second worksheet by its namecity_data <- read_excel("input.xlsx", sheet = "cities")print(city_data)When we execute the above code, it produces the following output:
# A tibble: 8 × 5 id name salary start_date dept <dbl> <chr> <dbl> <dttm> <chr>1 1 Rick 623. 2012-01-01 00:00:00 IT2 2 Dan 515. 2013-09-23 00:00:00 Operations3 3 Michelle 611 2014-11-15 00:00:00 IT4 4 Ryan 729 2014-05-11 00:00:00 HR5 5 Gary 843. 2015-03-27 00:00:00 Finance6 6 Nina 578 2013-05-21 00:00:00 IT7 7 Simon 633. 2013-07-30 00:00:00 Operations8 8 Guru 722. 2014-06-17 00:00:00 Finance
# A tibble: 8 × 2 name city <chr> <chr>1 Rick Seattle2 Dan Tampa3 Michelle Chicago4 Ryan Seattle5 Gary Houston6 Nina Boston7 Simon Mumbai8 Guru DallasStep 4: Writing Data to an Excel File
Section titled “Step 4: Writing Data to an Excel File”The writexl package makes writing data frames or tibbles to an .xlsx file incredibly simple with the write_xlsx() function.
Let’s imagine we’ve joined our two datasets and want to export the result.
# Load necessary librarieslibrary(writexl)library(dplyr)
# Join the two datasets we imported earlierfull_data <- left_join(employee_data, city_data, by = "name")
# Write the combined data to a new Excel file# The file will be created in your working directorywrite_xlsx(full_data, path = "output_data.xlsx")
cat("Successfully wrote combined data to output_data.xlsx")After running this code, a new file named output_data.xlsx will appear in your project folder, containing the merged data. You can even write multiple data frames to different sheets in the same file by passing them as a named list:
# Example of writing to multiple sheetslist_of_datasets <- list("Employees" = employee_data, "Locations" = city_data)write_xlsx(list_of_datasets, path = "multisheet_output.xlsx")