Import an Excel sheet
Now that you know the names of the sheets in the Excel file you want to import, it is time to import those sheets into R. You can do this with the read_excel()
function. Have a look at this recipe:
data <- read_excel("data.xlsx", sheet = "my_sheet")
This call simply imports the sheet with the name "my_sheet"
from the "data.xlsx"
file. You can also pass a number to the sheet
argument; this will cause read_excel()
to import the sheet with the given sheet number. sheet = 1
will import the first sheet, sheet = 2
will import the second sheet, and so on.
In this exercise, you'll continue working with the urbanpop.xlsx
(view) file.
This is a part of the course
“Introduction to Importing Data in R”
Exercise instructions
- The code to import the first and second sheets is already included. Can you add a command to also import the third sheet, and store the resulting data frame in
pop_3
? - Store the data frames
pop_1
,pop_2
andpop_3
in a list, that you callpop_list
. - Display the structure of
pop_list
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Read the sheets, one by one
pop_1 <- read_excel("urbanpop.xlsx", sheet = 1)
pop_2 <- read_excel("urbanpop.xlsx", sheet = 2)
# Put pop_1, pop_2 and pop_3 in a list: pop_list
pop_list <- list(___)
# Display the structure of pop_list
___
This exercise is part of the course
Introduction to Importing Data in R
In this course, you will learn to read CSV, XLS, and text files in R using tools like readxl and data.table.
Excel is a widely used data analysis tool. If you prefer to do your analyses in R, though, you'll need an understanding of how to import .csv data into R. This chapter will show you how to use readxl to do so.
Exercise 1: readxl (1)Exercise 2: List the sheets of an Excel fileExercise 3: Import an Excel sheetExercise 4: Reading a workbookExercise 5: readxl (2)Exercise 6: The col_names argumentExercise 7: The skip argumentWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.