Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

1. R Basics > Loading Data from an Excel File

Loading Data from an Excel File

Problem

You want to load data from an Excel file.

Solution

The xlsx package has the function read.xlsx() for reading Excel files. This will read the first sheet of an Excel spreadsheet:

# Only need to install once
install.packages("xlsx")

library(xslx)
data <- read.xlsx("datafile.xlsx", 1)

For reading older Excel files in the .xls format, the gdata package has the function read.xls():

# Only need to install once
install.packages("gdata")

library(gdata)
# Read first sheet
data <- read.xls("datafile.xls")

Discussion

With read.xlsx(), you can load from other sheets by specifying a number for sheetIndex or a name for sheetName:

data <- read.xlsx("datafile.xls", sheetIndex=2)

data <- read.xlsx("datafile.xls", sheetName="Revenues")

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint