library(dplyr)
library(tidyr)
Create a small dataset to test the reshape code.
events <- data_frame(
event_id = c(1001, 1002, 1003),
side_a = c("blue", "red", "green"),
side_b = c("red", "green", "blue"),
outcome = c("victory", "victory", "peace"),
winner = c(1, 2, 1)
)
The data we start with has three events:
events
| event_id | side_a | side_b | outcome | winner |
|---|---|---|---|---|
| 1001 | blue | red | victory | 1 |
| 1002 | red | green | victory | 2 |
| 1003 | green | blue | peace | 1 |
We reshape based on who won. The steps are:
side_a_won and side_b_won that indicate whether each side wonwin_status variableside variable to include the name of the actorvictory and peace from the outcome variableevents %>%
mutate(side_a_won = winner == 1,
side_b_won = winner == 2) %>%
gather(side, win_status, side_a_won, side_b_won) %>%
mutate(side = ifelse(side == "side_a_won", side_a, side_b)) %>%
mutate(victory = outcome == "victory",
peace = outcome == "peace") %>%
select(event_id, side, win_status, victory, peace) %>%
arrange(event_id)
| event_id | side | win_status | victory | peace |
|---|---|---|---|---|
| 1001 | blue | TRUE | TRUE | FALSE |
| 1001 | red | FALSE | TRUE | FALSE |
| 1002 | red | FALSE | TRUE | FALSE |
| 1002 | green | TRUE | TRUE | FALSE |
| 1003 | green | TRUE | FALSE | TRUE |
| 1003 | blue | FALSE | FALSE | TRUE |
And finally we get two observations for each event but a single actor per observation with their victory and outcome status