[R] 下載並讀取資料 — read.xlsx(), XML
Published in
3 min readDec 7, 2019
Coursera — Getting and Cleaning Data 的整理筆記
下載資料
- 開放資料下載處:Open Baltimore
有各式各樣的資料可以 play around - 確認資料夾存在,否則建立資料夾
從網站上下載資料
download.file(url, destfile, method)
method 的設定方式
- url 的開頭是
http
:直接使用download.file
,無需設定method
- url 的開頭是
https
+ Windows:直接使用download.file
,無需設定method
- url 的開頭是
https
+ Mac:設定method="curl"
提醒:要記錄下載資料的時間,以利於之後重新下載這筆資料
讀取資料
R 提供許多不同讀取資料的方式,列舉如下:
read.table()
read.xlsx()
,read.xlsx2()
- reading XML files
- reading JSON files
- The
data.table
package
read.xlsx()
- 讀取 Excel 檔案
.xlsx
的方式 library(xlsx)
- 參數:
read.xlsx(data, sheetIndex = 1, header=TRUE, colIndex=c(1:4), rowIndex=c(1:2)
- 寫入 Excel 檔案
.xlsx
:write.xlsx()
- 在讀取大型檔案時,
read.xlsx2
比read.xlsx
快 - The
XLConnect
package 提供更多處理.xlsx
的方式
Reading XML files
XML 是由markup
和content
所組成,markup
是建構資料結構的標籤,而content
是文件中真正的內容。
以下為來自 W3Schools XML 範例
markup
:<food> </food>
,<name> </name>
content
:Belgian Waffles
下載 XML
- 想要看XML資料內容的方式
- 想要更靈活的呼叫 XML file 中的內容,請參見 XPath 的使用方式
如果對你有幫助的話,別忘了拍手以表示鼓勵哦 。