在R中重塑data.frame

时间:2018-08-11 04:55:16

标签: r

我有以下示例数据集:

      Month   Model  Alert
3   January   Mod1     1
9   January   Mod2     0
15  January   Mod3     3
21  January   Mod4     3
27  January   Mod5     1
2  February   Mod1     0
8  February   Mod2     0
14 February   Mod3     2
20 February   Mod4     1
26 February   Mod5     1

如何有效地使用移调/整形功能来获得这种格式?

Model January February
Mod1    1        0
Mod2    0        0
Mod3    3        2
Mod4    3        1
Mod5    1        1

谢谢!

使用解决方案更新:

library(reshape2)
x <- melt(test, c("Month", "Model Name"), "Alert")
x2 <- dcast(test, `Model Name` ~ Month)

  Model Name January February March April May June
1   Mod1      1        0     1     2   0    2
2   Mod2       0        0     0     0   0    0
3   Mod3       3        2     2     2   3    2
4   Mod4       3        1     1     1   3    0
5   Mod5       1        1     2     1   1    2
6   Mod7      NA       NA    NA    NA   0   NA

0 个答案:

没有答案