require('tidyverse') require('magrittr') # import the data tall_index <- as_tibble(read.csv('tall_index.csv', fileEncoding="UTF-8-BOM")) grouping <- as_tibble(read.csv('grouping04.csv', fileEncoding="UTF-8-BOM")) # normalize the levels of the vectors for join combined <- sort(union(levels(tall_index$Country), levels(grouping$Country))) #create the joined file, filter our the countries that do not have a Level attached combined_tall_index <-tall_index %>% left_join(mutate(grouping, Country=factor(Country, levels=combined))) %>% filter(!is.na(Level)) #calculate the two averages countries_averages <- combined_tall_index %>% group_by(Country)%>% summarize(country_average = mean(fragile_states_index), Level= first(Level)) levels_averages <- combined_tall_index %>% group_by(Level)%>% summarize(level_average = mean(fragile_states_index)) # join back the table new_tall <- left_join(countries_averages, levels_averages) #add the new variable containing the difference new_tall %<>% mutate(difference= country_average - level_average) #write the countries list with a superior average over its level new_tall %>% filter(difference >= 0) %>% write_csv("positives_R.csv") # wirte the countries list with an average below its level group new_tall %>% filter(difference < 0) %>% write_csv("negatives_R.csv")