O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To connect to Excel using the xlsx package, follow the steps:

  1. Save a data frame to an Excel workbook:
> write.xlsx(auto, file = "auto.xlsx", sheetName =     "autobase", row.names = FALSE) 
  1. Add two new columns to the auto data frame:
> auto$kmpg <- auto$mpg * 1.6 
> auto$mpg_deviation <- (auto$mpg -     mean(auto$mpg))/auto$mpg 
  1. Create Excel objects such as workbooks, worksheets, rows, and cells:
> auto.wb <- createWorkbook() 
> sheet1 <- createSheet(auto.wb,"auto1") 
> rows <- createRow(sheet1, rowIndex=1) 
> cell.1 <- createCell(rows, colIndex=1)[[1,1]] 
> setCellValue(cell.1, "Hello Auto Data!") 
> addDataFrame(auto, sheet1, startRow=3, row.names=FALSE) 
  1. Assign styles to cells:
> cs <- CellStyle(auto.wb) + Font(auto.wb, isBold=TRUE, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required