2

I have this example dataset:

      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

How do I effectively use the transpose/reshape function to get this format?

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

Thank you!

Update with Solution:

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
Javier
  • 730
  • 4
  • 17
  • 1
    This is a simple long to wide conversion; use e.g. `library(tidyverse); df %>% spread(Month, Alert)`. More interesting details/discussions/options in the linked post. – Maurits Evers Aug 11 '18 at 07:36

0 Answers0