將寬格式轉換為長格式
我們已為你載入 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