Hello,
I am trying to create two new variables. One indicating the interdependence of Central Asian countries with China (depend_CN) and another with Russia (depend_RU)
For this, I sum Import and Export and divide by gdp, which are three other columns in the same dataset
I wrote this code, but it doesn't give me what I want. I should have NAs in depend_CN when the partner is Russia, but it calculates the interdependence for every case. Same for depend_RU
data_dependence$depend_RU <- dplyr::case_when(
data_dependence$Partner=="China" ~(data_dependence$depend_CN <- (data_dependence$Import+data_dependence$Export)/data_dependence$gdp),
data_dependence$Partner=="Russian Federation" ~(data_dependence$depend_RU <- (data_dependence$Import+data_dependence$Export)/data_dependence$gdp)
)
The reprex is:
data_dependence<-data.frame(
stringsAsFactors = FALSE,
Country = c("Uzbekistan","Uzbekistan",
"Uzbekistan","Uzbekistan","Uzbekistan","Uzbekistan",
"Uzbekistan","Uzbekistan","Uzbekistan","Uzbekistan"),
Year = c(2015,2016,2016,2017,2017,
2018,2018,2019,2019,2020),
gdp = c(81847410182,81779012351,
81779012351,59159945321,59159945321,50392607758,
50392607758,57921286440,57921286440,NA),
Partner = c("Russian Federation","China",
"Russian Federation","China","Russian Federation",
"China","Russian Federation","China","Russian Federation",
NA),
Import = c(2221187873,2007463677,
2092454055,2749423215,2858398083,3942096467,3317879453,
5044570600,3907969155,NA),
Export = c(575837496,1607057922,
777087867,1471448860,1046191944,2324394704,1063375312,
2180633878,1178739024,NA)
)
I appreciate your help on that!