R表重组

时间:2017-04-17 16:50:41

标签: r

我有一个具有以下结构的文件(这是组成数据):

Manufacturer    Compound          Concentration
hershey         Guar Gum          0.620142402
hershey         Starch                0.224585728
hershey         Glucose               0
hershey         Cocoa Powder          0.000982737
hershey         Citric Acid       0.846410424
hershey         Lactic Acid       0.050681621
hershey         Propylene Glycol      0.998148131
nestle          Guar Gum          0
nestle          Starch                0.857977057
nestle          Glucose               0.497836981
nestle          Cocoa Powder          0.461707584
nestle          Citric Acid       0
nestle          Lactic Acid       0.694891624
nestle          Propylene Glycol      0.367109663

如何使用R将其重组为:

Compound       Hershey          Nestle
Guar Gum          0.620142402   0
Starch        0.224585728   0.857977057
Glucose       0                 0.497836981
Cocoa Powder      0.000982737   0.461707584
Citric Acid       0.846410424   0
Lactic Acid       0.050681621   0.694891624
Propylene Glycol  0.998148131   0.367109663

谢谢

1 个答案:

答案 0 :(得分:-1)

这应该可以解决问题:

library("tidyverse")
df2 = spread(df, Manufacturer, Concentration)

Here's an article on reshaping data