将宽表转换为长表
已为您加载 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