開始使用免費開始

fread:進階用法

既然你已經了解 fread() 的基礎用法,接下來要認識這個函式的兩個引數:dropselect,用來捨棄或選取你關心的變數。

假設你有一個包含 5 個變數的資料集,而你只想保留第 1 與第 5 個變數,名稱分別是「a」和「e」。以下幾種寫法都可以:

fread("path/to/file.txt", drop = 2:4)
fread("path/to/file.txt", select = c(1, 5))
fread("path/to/file.txt", drop = c("b", "c", "d"))
fread("path/to/file.txt", select = c("a", "e"))

我們繼續用馬鈴薯資料吧,因為在 DataCamp 我們特別喜歡它。這份資料同樣位於 potatoes.csv 檔案中(view),其內容為以逗號分隔的紀錄。

本練習屬於課程

R 資料匯入入門

檢視課程

練習說明

  • 使用 fread(),並搭配 selectdrop 引數,只匯入平面檔中的 texturemoistness 欄位。它們分別對應到 "potatoes.csv" 的第 6 與第 8 欄。將結果存成變數 potatoes
  • plot() 繪製 potatoes 資料框的 2 個欄位:x 軸放 texture、y 軸放 moistness。請各使用一次美元符號寫法。你也可以自行命名座標軸與圖表。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import columns 6 and 8 of potatoes.csv: potatoes
potatoes <- ___

# Plot texture (x) and moistness (y) of potatoes
___
編輯並執行程式碼