0

希望你能帮助我的问题。

我的数据集看起来像这样。有 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

提前致谢!

4

1 回答 1

0

如果您不介意 tidyverse 方法,您可以执行以下操作:

library(tidyverse)
test <- read_delim(file=clipboard(), 
  col_names=TRUE, delim=",", trim_ws=TRUE) ## copied your 6 lines of data into the clipboard
test %>% 
  gather(key, value, -item, -index) %>% 
  unite("names", item, key) %>% 
  spread(names, value) %>% 
  mutate_at(vars(ends_with("_price")), as.numeric)
# A tibble: 2 x 9
  index apple_price apple_taste banana_price banana_taste orange_price
  <dbl>       <dbl> <chr>              <dbl> <chr>               <dbl>
1     1          NA NA                     4 medium                  3
2     2           4 medium                 3 medium                  4
# … with 3 more variables: orange_taste <chr>, pier_price <dbl>,
#   pier_taste <chr>

“gather”命令创建一个“long”数据帧,其中“key”变量包含存储信息的类型(即味道和价格),“value”变量包含实际值;由于值的类型不同,因此数值被强制转换为字符。“unite”合并到一个新列“names”项目描述和信息类型。"spread" 为 "names" 列的每个值创建一个具有相应值的新列。最后一位 (mutate_at) 将价格转换回数字格式。

于 2019-05-30T13:48:47.627 回答