[R] 下載並讀取資料 — read.xlsx(), XML

Min
Becoming a data scientist
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 檔案 .xlsxwrite.xlsx()
  • 在讀取大型檔案時,read.xlsx2read.xlsx
  • TheXLConnect package 提供更多處理 .xlsx 的方式

Reading XML files

XML 是由markupcontent所組成,markup是建構資料結構的標籤,而content是文件中真正的內容。

以下為來自 W3Schools XML 範例

  • markup: <food> </food>, <name> </name>
  • content: Belgian Waffles

下載 XML

  • 想要看XML資料內容的方式
  • 想要更靈活的呼叫 XML file 中的內容,請參見 XPath 的使用方式
圖片來源:https://www.w3schools.com/xml/xpath_syntax.asp

如果對你有幫助的話,別忘了拍手以表示鼓勵哦 。

參考資料

  1. https://data.baltimorecity.gov
  2. https://www.w3schools.com/xml/
  3. https://www.coursera.org/learn/data-cleaning/home/welcome

--

--