List and read Excel sheets
Just as readxl
and gdata
, you can use XLConnect
to import data from Excel file into R.
To list the sheets in an Excel file, use getSheets()
. To actually import data from a sheet, you can use readWorksheet()
. Both functions require an XLConnect workbook object as the first argument. When using the readWorksheet()
function, the sheet argument can accept either the name of the sheet (as a string) or the sheet's position number (as an integer) within the workbook.
You'll again be working with urbanpop.xlsx
(view). The my_book
object that links to this Excel file has already been created.
This exercise is part of the course
Introduction to Importing Data in R
Exercise instructions
- Print out the sheets of the Excel file that
my_book
links to. - Import the second sheet in
my_book
as a data frame. Print it out.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Build connection to urbanpop.xlsx
my_book <- loadWorkbook("urbanpop.xlsx")
# List the sheets in my_book
___
# Import the second sheet in my_book
___