變更 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.