將寬表轉換為長表
已為你載入 chocolate_wide 資料。請用 reshape() 把它轉成長表格式。我已替你先填好一些輸入,讓新資料的欄位標記正確。你需要補上幾個關鍵的輸入,告訴 reshape() 要如何轉換資料。
本練習屬於課程
R 的行銷選擇模型
練習說明
- 填入要開始的資料:
chocolate_wide。 - 填入想轉成的資料型態:
long。 - 填入每個選擇題中 3 個替代方案的
Brand、Price與Type所在欄位。你可能需要用str()或head()檢視資料來判斷。我已替你先填好Brand的欄位。 - 完成
reshape()指令後,使用head()確認資料已正確轉換。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# use reshape() to change the data from wide to long
chocolate <- reshape(data= ___ , direction="____",
varying = list(Brand=3:5, Price=____, Type=____),
v.names=c("Brand", "Price", "Type"), timevar="Alt")
# use head() to confirm that the data has been properly transformed