skip 參數
在讀取格式不那麼工整的 Excel 檔時,skip 是非常實用的參數。透過 skip,你可以告訴 R 忽略你要讀取之工作表前面的特定列數。看看以下範例:
read_excel("data.xlsx", skip = 15)
在這個例子中,"data.xlsx" 的第一個工作表前 15 列會被忽略。
如果該工作表的第一列原本包含欄名,這些資訊也會被 readxl 忽略。此時務必將 col_names 設為 FALSE,或是手動指定欄名!
你的目錄中已提供 urbanpop.xlsx(view),其欄名位於最前面的列。
本練習屬於課程
R 資料匯入入門
練習說明
- 匯入
"urbanpop.xlsx"的「第二個」工作表,但要略過前 21 列。記得設定col_names = FALSE。將讀入的資料框儲存為變數urbanpop_sel。 - 從
urbanpop_sel選取第一筆觀測並將其印出。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the second sheet of urbanpop.xlsx, skipping the first 21 rows: urbanpop_sel
urbanpop_sel <- read_excel("urbanpop.xlsx", sheet = ___, col_names =___, skip = ___)
# Print out the first observation from urbanpop_sel
___