I have two data-frames, one dyad-year and the other country-year.
Xccode1 ccode2 ccdistance countryname_1 countryname_2 majorpower_1
majorpower_2 milex_1 milper_1
1 1 2 20 0 United States of America Canada 1
0 143981000 2050
2 2 2 31 957 United States of America Bahamas 1
0 143981000 2050
3 3 2 40 1129 United States of America Cuba 1
0 143981000 2050
4 4 2 41 1437 United States of America Haiti 1
Country-Year:
ccode1 year Fac1_A Fac2_A Fac3_A
<int> <int> <dbl> <dbl> <dbl>
1 2 1980 -0.661 4.66 15.5
2 2 1981 -0.661 4.66 15.5
3 2 1982 -0.661 5.11 15.5
4 2 1983 -0.661 5.21 15.5
5 2 1984 -0.661 5.66 15.5
6 2 1985 -0.661 5.21 15.5
7 2 1986 -0.661 5.21 15.5
8 2 1987 -0.661 5.21 15.5
9 2 1988 -0.661 5.21 15.5
10 2 1989 -0.661 5.00 15.5
I'd like to merge this two data-frames so that each country in the dyad has a FacX value, however my attempts at doing this has either given me an error or lots of NA's. I first attempted to use a simple ifelse:
Demo_Dyad$Fac1_A_NR <- ifelse(Demo_Dyad$ccode1 == Cntry_yr$ccode1 &
Demo_Dyad$year == Cntry_yr$year,
Cntry_yr$Fac1_A, NA)
However, that results in each country in the Dyad_Year only having the value once. So e.g. USA <--> Haiti 1981 might have value X, but USA <--> Cuba 1981 will be NA.
I then attempted to do it by grouping in dplyr:
Demo_Dyad %>%
group_by(ccode1, year) %>%
mutate(Fac1_A_NR <- ifelse(ccode1 == Cntry_yr$ccode1 &
year == Cntry_yr$year, Cntry_yr$Fac1_A, NA))
But get the error: Error in `$<-.data.frame`(`*tmp*`, Fac1_A_NR, value = c(-0.660552389122193, :
replacement has 4942 rows, data has 217149
If anyone can see what is wrong with my code I would greatly appreciate it.