開始使用免費開始

變更 Selection 變數

把資料轉成長格式的最後一步,是調整儲存選擇結果的 Selection 欄。寬格式資料中,選擇是用整數表示被選到的替代方案。長格式中,則應該用邏輯值或 0-1 變數來標示哪個替代方案被選到。

本練習屬於課程

R 的行銷選擇模型

檢視課程

練習說明

  • 使用 head() 檢視包含選擇結果的 Selection 變數。注意 Selection 目前用整數代表被選到的替代方案編號。
  • chocolate$Alt == chocolate$Selection 指派給 chocolate$Selection
  • 再用 head() 檢視 Selection 變數如何被轉換。

動手互動練習

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

# Use head(chocolate) and look at the Selection variable. 


# Transform the Selection variable to a logical indicator
chocolate$Selection <- 

# Use head(chocolate) to see how the Selection variable has changed. Now it is logical.
編輯並執行程式碼