開始使用免費開始

將寬格式轉換為長格式

我們已為你載入 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
編輯並執行程式碼