reading sheets importing data in r
play

Reading sheets Importing Data in R Importing Data in R XLConnect - PowerPoint PPT Presentation

IMPORTING DATA IN R Reading sheets Importing Data in R Importing Data in R XLConnect Martin Studer Work with Excel through R Bridge between Excel and R XLS and XLSX Easy-to-use functionality Importing Data in R


  1. IMPORTING DATA IN R Reading sheets

  2. Importing Data in R

  3. Importing Data in R XLConnect ● Martin Studer ● Work with Excel through R ● Bridge between Excel and R ● XLS and XLSX ● Easy-to-use functionality

  4. Importing Data in R Installation Java class definitions > install.packages("XLConnect") also installing the dependencies 'XLConnectJars', 'rJava' ... R to Java interface ● Problems? ● Install Oracle's Java Development Kit (JDK) ● Google your error!

  5. Importing Data in R Capital Population loadWorkbook() New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Berlin 3382169 Stockholm 1683713 > library("XLConnect") Madrid 2938723 year_1990 Stockholm 1942362 > book <- loadWorkbook("cities.xlsx") year_2000 > str(book) Formal class 'workbook' [package "XLConnect"] 
 with 2 slots ..@ filename: chr "cities.xlsx" ..@ jobj : ...

  6. Importing Data in R Capital Population getSheets() New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Berlin 3382169 Stockholm 1683713 > getSheets(book) Madrid 2938723 year_1990 [1] "year_1990" "year_2000" Stockholm 1942362 year_2000 > library(readxl) > excel_sheets("cities.xlsx") [1] "year_1990" "year_2000"

  7. Importing Data in R Capital Population readWorksheet() New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Berlin 3382169 Stockholm 1683713 Madrid 2938723 year_1990 Stockholm 1942362 year_2000

  8. Importing Data in R Capital Population readWorksheet() New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Berlin 3382169 Stockholm 1683713 > readWorksheet(book, sheet = "year_2000") Madrid 2938723 year_1990 Stockholm 1942362 Capital Population year_2000 1 New York 17800000 2 Berlin 3382169 3 Madrid 2938723 4 Stockholm 1942362

  9. Importing Data in R readWorksheet() Capital Population New York 17800000 row 3 Berlin 3382169 row 4 Madrid 2938723 Stockholm 1942362 year_2000 col 2 > readWorksheet(book, sheet = "year_2000", 
 startRow = 3, endRow = 4, 
 startCol = 2, header = FALSE) Col1 1 3382169 2 2938723

  10. IMPORTING DATA IN R Let’s practice!

  11. IMPORTING DATA IN R Adapting sheets

  12. Importing Data in R New data! > pop_2010 <- data.frame( 
 Capital = c("New York", "Berlin", "Madrid", "Stockholm"), Population = c(8191900, 3460725, 3273000, 1372565)) > pop_2010 Capital Population 1 New York 8191900 2 Berlin 3460725 3 Madrid 3273000 4 Stockholm 1372565

  13. Importing Data in R createSheet() > pop_2010 <- ... # truncated > library(XLConnect) > book <- loadWorkbook("cities.xlsx") > createSheet(book, name = "year_2010") Capital Population New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Berlin 3382169 Stockholm 1683713 Madrid 2938723 year_1990 Stockholm 1942362 year_2000

  14. Importing Data in R createSheet() > pop_2010 <- ... # truncated > library(XLConnect) > book <- loadWorkbook("cities.xlsx") > createSheet(book, name = "year_2010") Capital Population New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Berlin 3382169 Stockholm 1683713 Madrid 2938723 year_1990 Stockholm 1942362 year_2000 year_2010

  15. Importing Data in R writeWorksheet() > pop_2010 <- ... # truncated > library(XLConnect) > book <- loadWorkbook("cities.xlsx") > createSheet(book, name = "year_2010") > writeWorksheet(book, pop_2010, sheet = "year_2010") Capital Population New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Berlin 3382169 Stockholm 1683713 Madrid 2938723 year_1990 Stockholm 1942362 year_2000 year_2010

  16. Importing Data in R writeWorksheet() > pop_2010 <- ... # truncated > library(XLConnect) > book <- loadWorkbook("cities.xlsx") > createSheet(book, name = "year_2010") > writeWorksheet(book, pop_2010, sheet = "year_2010") Capital Population New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Capital Population Berlin 3382169 Stockholm 1683713 New York 17800000 Madrid 2938723 year_1990 Berlin 3382169 Stockholm 1942362 Madrid 2938723 year_2000 Stockholm 1942362 year_2010

  17. Importing Data in R saveWorkbook() > pop_2010 <- ... # truncated > library(XLConnect) > book <- loadWorkbook("cities.xlsx") > createSheet(book, name = "year_2010") > writeWorksheet(book, pop_2010, sheet = "year_2010") > saveWorkbook(book, file = "cities2.xlsx") Capital Population cities2.xlsx New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Capital Population Berlin 3382169 Stockholm 1683713 New York 17800000 Madrid 2938723 year_1990 Berlin 3382169 Stockholm 1942362 Madrid 2938723 year_2000 Stockholm 1942362 year_2010

  18. Importing Data in R renameSheet() > renameSheet(book, "year_1990", "Y1990") > renameSheet(book, "year_2000", "Y2000") > renameSheet(book, "year_2010", "Y2010") > saveWorkbook(book, file = "cities3.xlsx") Capital Population New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Capital Population Berlin 3382169 Stockholm 1683713 New York 17800000 Madrid 2938723 year_1990 Berlin 3382169 Stockholm 1942362 Madrid 2938723 year_2000 Stockholm 1942362 year_2010

  19. Importing Data in R renameSheet() > renameSheet(book, "year_1990", "Y1990") > renameSheet(book, "year_2000", "Y2000") > renameSheet(book, "year_2010", "Y2010") > saveWorkbook(book, file = "cities3.xlsx") Capital Population cities3.xlsx New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Capital Population Berlin 3382169 Stockholm 1683713 New York 17800000 Madrid 2938723 Y1990 Berlin 3382169 Stockholm 1942362 Madrid 2938723 Y2000 Stockholm 1942362 Y2010

  20. Importing Data in R removeSheet() > removeSheet(book, sheet = "Y2010") > saveWorkbook(book, file = "cities4.xlsx") Capital Population New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Capital Population Berlin 3382169 Stockholm 1683713 New York 17800000 Madrid 2938723 Y1990 Berlin 3382169 Stockholm 1942362 Madrid 2938723 Y2000 Stockholm 1942362 Y2010

  21. Importing Data in R removeSheet() > removeSheet(book, sheet = "Y2010") > saveWorkbook(book, file = "cities4.xlsx") Capital Population cities4.xlsx New York 16044000 Capital Population Berlin 3433695 New York 17800000 Madrid 3010492 Berlin 3382169 Stockholm 1683713 Madrid 2938723 Y1990 Stockholm 1942362 Y2000

  22. Importing Data in R Wrap-up ● Basic operations ● Reproducibility is the key! ● More functionality ● Styling cells ● Working with formulas ● Arranging cells ● ...

  23. IMPORTING DATA IN R Let’s practice!

Recommend


More recommend