開始使用免費開始

存取與取用資料框(2)

如你所料,從資料框選取特定欄位是很常見的操作。常見到甚至有自己的速記符號 $。以下兩種寫法會得到相同的結果:

cash$cash_flow

[1] 1000 4000  550 1500 1100  750 6000

cash[,"cash_flow"]

[1] 1000 4000  550 1500 1100  750 6000

很實用吧?來試試看!

本練習屬於課程

R for Finance 入門

檢視課程

練習說明

  • 使用 $cash 選取 "year" 欄位。
  • 使用 $cash 選取 "cash_flow" 欄位,並將它乘以 2。
  • 指派 NULL 可以刪除欄位。執行刪除 "company" 的程式碼。
  • 再次印出 cash

動手互動練習

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

# Select the year column


# Select the cash_flow column and multiply by 2


# Delete the company column
cash$company <- NULL

# Print cash again
編輯並執行程式碼