Customize readWorksheet
To get a clear overview about urbanpop.xlsx (view) without having to open up the Excel file, you can execute the following code:
my_book <- loadWorkbook("urbanpop.xlsx")
sheets <- getSheets(my_book)
all <- lapply(sheets, readWorksheet, object = my_book)
str(all)
Suppose we're only interested in urban population data of the years 1968, 1969 and 1970. The data for these years is in the columns 3, 4, and 5 of the second sheet. Only selecting these columns will leave us in the dark about the actual countries the figures belong to.
Bu egzersiz
Introduction to Importing Data in R
kursunun bir parçasıdırEgzersiz talimatları
- Extend the
readWorksheet()command with thestartColandendColarguments to only import the columns 3, 4, and 5 of the second sheet. urbanpop_selno longer contains information about the countries now. Can you write anotherreadWorksheet()command that imports only the first column from the second sheet? Store the resulting data frame ascountries.- Use
cbind()to paste togethercountriesandurbanpop_sel, in this order. Store the result asselection.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# Build connection to urbanpop.xlsx
my_book <- loadWorkbook("urbanpop.xlsx")
# Import columns 3, 4, and 5 from second sheet in my_book: urbanpop_sel
urbanpop_sel <- readWorksheet(my_book, sheet = 2, ___, ___)
# Import first column from second sheet in my_book: countries
countries <- ___
# cbind() urbanpop_sel and countries together: selection
selection <- ___