更改 Selection 变量
将数据转换为长格式的最后一步是修改存储选择结果的 Selection 列。在宽格式数据中,选择结果以整数表示所选的备选项编号。在长格式中,它应为逻辑型或 0-1 变量,用于标记哪个备选项被选中。
本练习是课程的一部分
R 中的营销选择建模
练习说明
- 使用
head()查看包含选择结果的Selection变量。请注意,Selection中是所选备选项的编号(整数)。 - 将
chocolate$Alt == chocolate$Selection赋值给chocolate$Selection。 - 使用
head()查看Selection变量是如何被转换的。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Use head(chocolate) and look at the Selection variable.
# Transform the Selection variable to a logical indicator
chocolate$Selection <-
# Use head(chocolate) to see how the Selection variable has changed. Now it is logical.