Using the xlsx package to connect to Excel
There are multiple packages to connect Excel with R; in this recipe, we discuss the xlsx
package. Other commonly used packages are RExcel
and XLConnect
.
Getting ready
If you have not already downloaded the files for this chapter, do it now and ensure that the files are in your R working directory:
- Install
xlsx
usinginstall.packages("xlsx")
- Load the library using
library(xlsx)
- Read the data:
> auto <- read.csv("auto-mpg.csv", stringsAsFactors=FALSE)
How to do it...
To connect to Excel using the xlsx
package, follow the steps:
- Save a data frame to an Excel workbook:
> write.xlsx(auto, file = "auto.xlsx", sheetName = "autobase", row.names = FALSE)
- Add two new columns to the auto data frame:
> auto$kmpg <- auto$mpg ...
Get R: Recipes for Analysis, Visualization and Machine Learning now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.