Skip to content

Commit ad7425e

Browse files
committed
should have saved already
1 parent c7ee687 commit ad7425e

5 files changed

+174
-68
lines changed

transform_and_merge_01.Rmd

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ output: html_notebook
55

66
**Goal:** identify countries on opposite sides
77

8+
## Disclaimers
9+
10+
- User should verify the results
11+
- There is a potential to generate more concise code. Meanwhile, this appears to work just fine.
12+
813
## Load Libraries
914

10-
```{r}
15+
```{r loadpackages}
1116
library(tidyverse)
1217
```
1318

1419

1520
## Example Data
1621

17-
```{r}
18-
country <- c("A","B","C","D","A","E","F","A","C","D","G","H")
22+
```{r sampledata}
23+
country <- c("A","B","C","D","A","E","F","X","Y","Z","Q","R")
1924
dispute <- c(1,1,1,1,2,2,2,3,3,3,3,3)
20-
side_a <- c(0,0,1,1,0,1,1,0,1,0,1,0)
25+
side_a <- c(0,0,1,1,0,1,1,0,0,0,1,1)
2126
side_b <- c(1,1,0,0,1,0,0,1,1,1,0,0)
2227
2328
country_df <- data_frame(country, dispute, side_a, side_b)
@@ -30,7 +35,7 @@ country_df
3035

3136
### Side B Countries
3237

33-
```{r}
38+
```{r sidea}
3439
side_a <- country_df %>%
3540
filter(side_a == 0)
3641
@@ -39,7 +44,7 @@ side_a
3944

4045
### Side A Countries
4146

42-
```{r}
47+
```{r sideb}
4348
side_b <- country_df %>%
4449
filter(side_a == 1)
4550
@@ -49,41 +54,43 @@ side_b
4954

5055
## Transform Data ; Build Data Frame
5156

52-
```{r}
57+
```{r for_df}
5358
5459
# declare Tibble (data frame)
5560
df_total = tibble()
5661
5762
for (i in 1:max(country_df$dispute)) {
58-
# Build vector of disputants for each Dispute Category
63+
# Build vector of disputants (multi-valued cell: country_y)
5964
country_y <- country_df %>%
6065
filter(dispute == i) %>%
6166
select(-dispute) %>%
62-
mutate(side_a = if_else(side_a == 0, country, NA_character_),
63-
side_b = if_else(side_b == 0, country, NA_character_)) %>%
67+
mutate(side_b = if_else(side_b == 0, country, NA_character_)) %>%
6468
select(side_b) %>%
65-
na.omit() %>%
69+
drop_na() %>%
6670
unique() %>%
67-
.[[1]] %>%
68-
paste(., collapse = "|")
71+
as_vector() %>%
72+
paste0(collapse = "|")
6973
7074
# Build a data frame that adds the disputants vector as a column-variable;
71-
# drops blank rows ; then split the multi-valued cells (disputants)
75+
# drops blank rows ; separate the multi-valued cells (disputants/country_y)
76+
# into rows
7277
country_df_temp <- country_df %>%
7378
filter(dispute == i) %>%
74-
mutate(side_a = if_else(side_a == 0, country, NA_character_),
75-
country_disputes = if_else(side_b == 1, country_y, NA_character_)) %>%
76-
select(country, country_disputes) %>%
79+
mutate(country_disputes = if_else(side_b == 1, country_y, NA_character_)) %>%
7780
drop_na() %>%
78-
separate_rows(country_disputes, convert = TRUE) %>%
79-
arrange(country_disputes) %>%
80-
mutate(Dispute = i)
81+
separate_rows(country_disputes, convert = TRUE)
8182
8283
# Append big Dataframe from from the iterated dataframes
8384
df_total <- bind_rows(df_total, country_df_temp)
8485
8586
}
8687
87-
df_total
88+
df_total%>%
89+
arrange(country_disputes) %>%
90+
rename(country_x = country,
91+
counrty_y = country_disputes) %>%
92+
select(dispute, counrty_y, country_x)
93+
94+
```
95+
8896

89-
```

0 commit comments

Comments
 (0)