fread:進階用法
既然你已經了解 fread() 的基礎用法,接下來要認識這個函式的兩個引數:drop 和 select,用來捨棄或選取你關心的變數。
假設你有一個包含 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(),並搭配select或drop引數,只匯入平面檔中的texture與moistness欄位。它們分別對應到"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
___