R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
169: Reading Excel Files with readxl
A few years ago, I spent an entire Friday afternoon fighting with a "consolidated" spreadsheet from a client. They had a master workbook with twelve different tabs—one for each month of the year—and each tab had three rows of useless metadata (like "Report Date" and "Author") before the actual data table even started. I tried exporting them to CSVs first, but the formatting broke, and I ended up with a mess of shifted columns. I realized then that trying to force Excel data into a text format before reading it into R is a waste of time. You need a tool that speaks Excel's native language.
That's where the readxl package comes in. It's part of the tidyverse, but it's a bit unique because it doesn't have any external dependencies. It just does one thing very well: it pulls .xls and .xlsx files directly into R as tibbles. Since you've already worked with data frames and tibbles in previous lessons, the output will feel familiar, but the input process requires a bit of precision to get right.
Dealing with Metadata and Offset Rows
Most "real world" Excel files aren't clean. They often have titles or notes at the top that will confuse R if you just call read_excel(). If R sees a title in the first row, it'll assume that's your column header, and your actual data will be shifted. To fix this, use the skip argument.
library(readxl)
# Skip the first 3 rows of "junk" and start reading at row 4
budget_data <- read_excel("company_budget.xlsx", skip = 3)
I also recommend being explicit about your column types. By default, readxl guesses the data type based on the first thousand rows. But if a column is mostly empty at the top and suddenly has numbers at row 1001, R might guess it's a character column and throw a warning. You can pass a vector to col_types—using "text", "numeric", "date", or "skip"—to force R to treat the columns exactly how you intend.
Targeting Specific Sheets and Ranges
The biggest advantage of readxl is how it handles workbooks with multiple tabs. You don't have to save every sheet as a separate file. You can refer to a sheet by its name (a string) or its position (an integer).
# Read the second tab in the workbook
q2_data <- read_excel("sales_report.xlsx", sheet = 2)
# Or better yet, use the actual name so your code doesn't break if someone moves the tabs
q2_data <- read_excel("sales_report.xlsx", sheet = "Quarter 2")
Sometimes, you don't even want the whole sheet. Maybe there's a small summary table tucked away in cells A10 through C15. In those cases, you can use the range argument, which uses standard Excel notation. It's a lifesaver when you're dealing with those "dashboard" style spreadsheets where data is scattered across the page.
# Grab only the summary table from a specific area
summary_stats <- read_excel("sales_report.xlsx", range = "A10:C15")
One quick tip: if you're working with a massive workbook and you aren't sure what the sheets are named, use excel_sheets("your_file.xlsx"). It returns a character vector of all the tab names, which you can then plug into a loop or a map function to import everything at once.
📋 Practical Task
Parsing the Regional Sales Workbook
You have been handed an Excel file named regional_sales.xlsx. The file has a tab named "West_Coast". However, the actual data table doesn't start until row 5 (the first 4 rows are just the company logo and report headers). Additionally, the "West_Coast" sheet has a small "Regional Totals" table located in the range G2:H5.
Write the R code to perform the following:
- Load the
readxllibrary. - Import the main data table from the
"West_Coast"sheet, skipping the first 4 rows, and assign it to a variable calledwest_sales. - Import the summary totals from the range
G2:H5of that same sheet and assign it to a variable calledwest_totals.
There are no comments for now.