開始使用免費開始

將寬表轉換為長表

已為你載入 chocolate_wide 資料。請用 reshape() 把它轉成長表格式。我已替你先填好一些輸入,讓新資料的欄位標記正確。你需要補上幾個關鍵的輸入,告訴 reshape() 要如何轉換資料。

本練習屬於課程

R 的行銷選擇模型

檢視課程

練習說明

  • 填入要開始的資料:chocolate_wide
  • 填入想轉成的資料型態:long
  • 填入每個選擇題中 3 個替代方案的 BrandPriceType 所在欄位。你可能需要用 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
編輯並執行程式碼