希望你能帮助我的问题。
我的数据集看起来像这样。有 1500 行。
+-------+---------+---------+--------+
| index | item | taste | price |
+-------+---------+---------+--------+
| 1 | orange | low | 3 |
| 1 | banana | medium | 4 |
| 1 | pier | high | 2 |
| 2 | apple | medium | 4 |
| 2 | orange | medium | 4 |
| 2 | banana | medium | 3 |
| ... | ... | ... | ... |
| 1500 | 1500 | 1500 | 1500 |
+-------+---------+---------+--------+
我使用此代码但收到错误消息:
library(data.table)
dcast(setDT(df), index ~ item, value.var = c("taste", "price"))
错误按摩:
.subset2(x, i, exact = exact) 中的错误:下标超出范围此外:警告消息:在 if (!(value.var %in% names(data))) { 中:条件长度 > 1 并且只使用第一个元素
这是我想要得到的预期结果。
index, item, taste_orange,taste.banana, taste.pier, taste.apple, price_orange,price.banana, price.pier, price.apple,
1, low,medium,high,0,3,4,2,0
2, medium,medium,0,medium,4,3,0,4
...,...,...,...,...,...,...,....,...
1500,1500, 1500,1500,1500,1500,1500,1500,1500
提前致谢!