library(dplyr)
library(knitr)
world <- read.csv("world-small.csv")
head(world)
## country region gdppcap08 polityIV
## 1 Albania C&E Europe 7715 17.8
## 2 Algeria Africa 8033 10.0
## 3 Angola Africa 5899 8.0
## 4 Argentina S. America 14333 18.0
## 5 Armenia C&E Europe 6070 15.0
## 6 Australia Asia-Pacific 35677 20.0
require(plyr)
require(dplyr)
require(tidyr)
2 Subset world to European countries. Save this subset as a new data frame called Europe
world
## country region gdppcap08 polityIV
## 1 Albania C&E Europe 7715 17.800000
## 2 Algeria Africa 8033 10.000000
## 3 Angola Africa 5899 8.000000
## 4 Argentina S. America 14333 18.000000
## 5 Armenia C&E Europe 6070 15.000000
## 6 Australia Asia-Pacific 35677 20.000000
## 7 Austria W. Europe 38152 20.000000
## 8 Azerbaijan C&E Europe 8765 3.000000
## 9 Bahrain Middle East 34605 3.000000
## 10 Bangladesh Asia-Pacific 1334 16.000000
## 11 Belarus C&E Europe 12261 3.000000
## 12 Belgium W. Europe 34493 20.000000
## 13 Benin Africa 1468 16.200000
## 14 Bhutan Asia-Pacific 4755 2.000000
## 15 Bolivia S. America 4278 18.200000
## 16 Botswana Africa 13392 19.000000
## 17 Brazil S. America 10296 18.000000
## 18 Bulgaria C&E Europe 12393 19.000000
## 19 Burkina Faso Africa 1161 10.000000
## 20 Cambodia Asia-Pacific 1905 12.000000
## 21 Cameroon Africa 2215 6.000000
## 22 Canada N. America 36444 20.000000
## 23 Central African Republic Africa 736 10.200000
## 24 Chad Africa 1455 8.000000
## 25 Chile S. America 14465 19.200000
## 26 China Asia-Pacific 5962 3.000000
## 27 Colombia S. America 8885 17.000000
## 28 Comoros Africa 1169 15.800000
## 29 Congo Brazzaville Africa 3946 6.000000
## 30 Congo Kinshasa Africa 321 15.000000
## 31 Costa Rica S. America 11241 20.000000
## 32 Croatia C&E Europe 19084 18.400000
## 33 Cuba S. America 9500 3.000000
## 34 Czech Republic C&E Europe 24712 20.000000
## 35 Denmark Scandinavia 36607 20.000000
## 36 Djibouti Africa 2140 12.000000
## 37 Ecuador S. America 8009 16.200000
## 38 Egypt Middle East 5416 4.000000
## 39 El Salvador S. America 6794 17.000000
## 40 Equatorial Guinea Africa 33873 5.000000
## 41 Eritrea Africa 632 3.000000
## 42 Estonia C&E Europe 20662 16.000000
## 43 Fiji Asia-Pacific 4382 13.800000
## 44 Finland Scandinavia 35427 20.000000
## 45 France W. Europe 34045 19.000000
## 46 Gabon Africa 14527 6.000000
## 47 Gambia Africa 1363 5.000000
## 48 Georgia C&E Europe 4896 15.666667
## 49 Germany W. Europe 35613 20.000000
## 50 Ghana Africa 1452 16.666667
## 51 Greece W. Europe 29361 20.000000
## 52 Guatemala S. America 4760 18.000000
## 53 Guinea Africa 1204 9.000000
## 54 Guinea-Bissau Africa 538 11.000000
## 55 Guyana S. America 2542 16.000000
## 56 Haiti S. America 1177 8.000000
## 57 Honduras S. America 3965 17.000000
## 58 Hungary C&E Europe 19330 20.000000
## 59 India Asia-Pacific 2972 19.000000
## 60 Indonesia Asia-Pacific 3975 16.666667
## 61 Iran Middle East 11666 10.000000
## 62 Iraq Middle East 3570 1.000000
## 63 Ireland W. Europe 44200 20.000000
## 64 Israel Middle East 27548 20.000000
## 65 Italy W. Europe 30756 20.000000
## 66 Jamaica S. America 7705 19.000000
## 67 Japan Asia-Pacific 34099 20.000000
## 68 Jordan Middle East 5283 8.000000
## 69 Kazakhstan C&E Europe 11315 4.000000
## 70 Kenya Africa 1590 18.000000
## 71 Korea South Asia-Pacific 27939 18.000000
## 72 Kuwait Middle East 39914 3.000000
## 73 Kyrgyzstan C&E Europe 2188 7.000000
## 74 Laos Asia-Pacific 2134 3.000000
## 75 Latvia C&E Europe 17100 18.000000
## 76 Lesotho Africa 1588 18.000000
## 77 Liberia Africa 388 10.000000
## 78 Libya Middle East 15402 3.000000
## 79 Lithuania C&E Europe 18824 20.000000
## 80 Macedonia C&E Europe 10041 19.000000
## 81 Madagascar Africa 1049 17.000000
## 82 Malawi Africa 837 15.000000
## 83 Malaysia Asia-Pacific 14215 13.000000
## 84 Mali Africa 1128 16.000000
## 85 Mauritania Africa 2052 4.000000
## 86 Mauritius Africa 12079 20.000000
## 87 Mexico N. America 14495 18.000000
## 88 Moldova C&E Europe 2925 18.000000
## 89 Mongolia Asia-Pacific 3566 20.000000
## 90 Morocco Middle East 4388 4.000000
## 91 Mozambique Africa 855 16.000000
## 92 Namibia Africa 6343 16.000000
## 93 Nepal Asia-Pacific 1112 4.000000
## 94 Netherlands W. Europe 40849 20.000000
## 95 New Zealand Asia-Pacific 27029 20.000000
## 96 Nicaragua S. America 2682 18.000000
## 97 Niger Africa 684 15.333333
## 98 Nigeria Africa 2082 14.000000
## 99 Norway Scandinavia 58138 20.000000
## 100 Oman Middle East 22478 2.000000
## 101 Pakistan Asia-Pacific 2644 5.000000
## 102 Papua New Guinea Asia-Pacific 2208 20.000000
## 103 Paraguay S. America 4709 17.666667
## 104 Peru S. America 8507 19.000000
## 105 Philippines Asia-Pacific 3510 18.000000
## 106 Poland C&E Europe 17625 20.000000
## 107 Portugal W. Europe 23074 20.000000
## 108 Qatar Middle East 85868 0.000000
## 109 Romania C&E Europe 14065 18.333333
## 110 Russia C&E Europe 16139 17.000000
## 111 Rwanda Africa 1022 6.666667
## 112 Saudi Arabia Middle East 23920 0.000000
## 113 Senegal Africa 1772 18.000000
## 114 Sierra Leone Africa 766 15.000000
## 115 Singapore Asia-Pacific 49284 8.000000
## 116 Slovakia C&E Europe 22081 19.000000
## 117 Slovenia C&E Europe 27605 20.000000
## 118 Solomon Islands Asia-Pacific 2610 18.000000
## 119 South Africa Africa 10109 19.000000
## 120 Spain W. Europe 31954 20.000000
## 121 Sri Lanka Asia-Pacific 4560 15.333333
## 122 Sudan Africa 2153 4.000000
## 123 Swaziland Africa 4928 1.000000
## 124 Sweden Scandinavia 37383 20.000000
## 125 Switzerland W. Europe 42536 20.000000
## 126 Taiwan Asia-Pacific 30881 19.333333
## 127 Tajikistan C&E Europe 1906 7.666667
## 128 Tanzania Africa 1263 11.000000
## 129 Thailand Asia-Pacific 7703 19.000000
## 130 Togo Africa 829 8.000000
## 131 Tunisia Middle East 7996 6.000000
## 132 Turkey Middle East 13920 17.000000
## 133 Turkmenistan C&E Europe 6641 1.000000
## 134 UAE Middle East 38830 2.000000
## 135 Uganda Africa 1165 6.000000
## 136 Ukraine C&E Europe 7271 16.000000
## 137 United Kingdom W. Europe 35445 20.000000
## 138 United States N. America 46716 20.000000
## 139 Uruguay S. America 12734 20.000000
## 140 Uzbekistan C&E Europe 2656 1.000000
## 141 Venezuela S. America 12804 16.000000
## 142 Vietnam Asia-Pacific 2785 3.000000
## 143 Yemen Middle East 2400 8.000000
## 144 Zambia Africa 1356 15.000000
## 145 Zimbabwe Africa 188 6.000000
unique(world$region)
## [1] "C&E Europe" "Africa" "S. America" "Asia-Pacific" "W. Europe"
## [6] "Middle East" "N. America" "Scandinavia"
# data.frame "Europe": "C&E Europe", "W. Europe", "Scandinavia"
europe <- filter(world, region == c("C&E Europe", "W. Europe", "Scandinavia"))
europe
## country region gdppcap08 polityIV
## 1 Albania C&E Europe 7715 17.800000
## 2 Czech Republic C&E Europe 24712 20.000000
## 3 Hungary C&E Europe 19330 20.000000
## 4 Italy W. Europe 30756 20.000000
## 5 Kyrgyzstan C&E Europe 2188 7.000000
## 6 Lithuania C&E Europe 18824 20.000000
## 7 Moldova C&E Europe 2925 18.000000
## 8 Norway Scandinavia 58138 20.000000
## 9 Poland C&E Europe 17625 20.000000
## 10 Portugal W. Europe 23074 20.000000
## 11 Romania C&E Europe 14065 18.333333
## 12 Switzerland W. Europe 42536 20.000000
## 13 Tajikistan C&E Europe 1906 7.666667
## 14 Turkmenistan C&E Europe 6641 1.000000
## 15 Ukraine C&E Europe 7271 16.000000
## 16 United Kingdom W. Europe 35445 20.000000
3 Add two variables to europe: #A. A variable that recodes polityIV from 0-20 to -10-10. #B. A variable that categorizes a country as "rich" or "poor" based on some cutoff of gdppcap08 you think is reasonable.
europe %>% arrange(desc(gdppcap08))
## country region gdppcap08 polityIV
## 1 Norway Scandinavia 58138 20.000000
## 2 Switzerland W. Europe 42536 20.000000
## 3 United Kingdom W. Europe 35445 20.000000
## 4 Italy W. Europe 30756 20.000000
## 5 Czech Republic C&E Europe 24712 20.000000
## 6 Portugal W. Europe 23074 20.000000
## 7 Hungary C&E Europe 19330 20.000000
## 8 Lithuania C&E Europe 18824 20.000000
## 9 Poland C&E Europe 17625 20.000000
## 10 Romania C&E Europe 14065 18.333333
## 11 Albania C&E Europe 7715 17.800000
## 12 Ukraine C&E Europe 7271 16.000000
## 13 Turkmenistan C&E Europe 6641 1.000000
## 14 Moldova C&E Europe 2925 18.000000
## 15 Kyrgyzstan C&E Europe 2188 7.000000
## 16 Tajikistan C&E Europe 1906 7.666667
europe_new <- europe %>%
mutate(europe, polityIV - 10) %>%
mutate(status = ifelse(gdppcap08 >= 12000, "Rich", "Poor"))
europe_new
## country region gdppcap08 polityIV status
## 1 Albania C&E Europe 7715 17.800000 Poor
## 2 Czech Republic C&E Europe 24712 20.000000 Rich
## 3 Hungary C&E Europe 19330 20.000000 Rich
## 4 Italy W. Europe 30756 20.000000 Rich
## 5 Kyrgyzstan C&E Europe 2188 7.000000 Poor
## 6 Lithuania C&E Europe 18824 20.000000 Rich
## 7 Moldova C&E Europe 2925 18.000000 Poor
## 8 Norway Scandinavia 58138 20.000000 Rich
## 9 Poland C&E Europe 17625 20.000000 Rich
## 10 Portugal W. Europe 23074 20.000000 Rich
## 11 Romania C&E Europe 14065 18.333333 Rich
## 12 Switzerland W. Europe 42536 20.000000 Rich
## 13 Tajikistan C&E Europe 1906 7.666667 Poor
## 14 Turkmenistan C&E Europe 6641 1.000000 Poor
## 15 Ukraine C&E Europe 7271 16.000000 Poor
## 16 United Kingdom W. Europe 35445 20.000000 Rich
4 Drop the region variable in europe (keep the rest).
europe %>% select(-region)
## country gdppcap08 polityIV
## 1 Albania 7715 17.800000
## 2 Czech Republic 24712 20.000000
## 3 Hungary 19330 20.000000
## 4 Italy 30756 20.000000
## 5 Kyrgyzstan 2188 7.000000
## 6 Lithuania 18824 20.000000
## 7 Moldova 2925 18.000000
## 8 Norway 58138 20.000000
## 9 Poland 17625 20.000000
## 10 Portugal 23074 20.000000
## 11 Romania 14065 18.333333
## 12 Switzerland 42536 20.000000
## 13 Tajikistan 1906 7.666667
## 14 Turkmenistan 6641 1.000000
## 15 Ukraine 7271 16.000000
## 16 United Kingdom 35445 20.000000
5 Sort europe based on Polity IV.
europe %>% arrange(desc(polityIV))
## country region gdppcap08 polityIV
## 1 Czech Republic C&E Europe 24712 20.000000
## 2 Hungary C&E Europe 19330 20.000000
## 3 Italy W. Europe 30756 20.000000
## 4 Lithuania C&E Europe 18824 20.000000
## 5 Norway Scandinavia 58138 20.000000
## 6 Poland C&E Europe 17625 20.000000
## 7 Portugal W. Europe 23074 20.000000
## 8 Switzerland W. Europe 42536 20.000000
## 9 United Kingdom W. Europe 35445 20.000000
## 10 Romania C&E Europe 14065 18.333333
## 11 Moldova C&E Europe 2925 18.000000
## 12 Albania C&E Europe 7715 17.800000
## 13 Ukraine C&E Europe 7271 16.000000
## 14 Tajikistan C&E Europe 1906 7.666667
## 15 Kyrgyzstan C&E Europe 2188 7.000000
## 16 Turkmenistan C&E Europe 6641 1.000000
6 Repeat Exercises 2-5 using chaining.
europe_1 <- world %>%
filter(region == c("C&E Europe", "W. Europe", "Scandinavia"))%>%
arrange(desc(gdppcap08)) %>%
mutate(status = ifelse(gdppcap08 >= 12000, "Rich", "Poor")) %>%
select(-region) %>%
arrange(desc(polityIV))
europe_1
## country gdppcap08 polityIV status
## 1 Norway 58138 20.000000 Rich
## 2 Switzerland 42536 20.000000 Rich
## 3 United Kingdom 35445 20.000000 Rich
## 4 Italy 30756 20.000000 Rich
## 5 Czech Republic 24712 20.000000 Rich
## 6 Portugal 23074 20.000000 Rich
## 7 Hungary 19330 20.000000 Rich
## 8 Lithuania 18824 20.000000 Rich
## 9 Poland 17625 20.000000 Rich
## 10 Romania 14065 18.333333 Rich
## 11 Moldova 2925 18.000000 Poor
## 12 Albania 7715 17.800000 Poor
## 13 Ukraine 7271 16.000000 Poor
## 14 Tajikistan 1906 7.666667 Poor
## 15 Kyrgyzstan 2188 7.000000 Poor
## 16 Turkmenistan 6641 1.000000 Poor
7 What was the world's mean GDP per capita in 2008? Polity IV score?
mean(world[ ,3])
## [1] 13251.99
mean(world[ ,4])
## [1] 13.40782
8 What was Africa's mean GDP per capita and Polity IV score?
world
## country region gdppcap08 polityIV
## 1 Albania C&E Europe 7715 17.800000
## 2 Algeria Africa 8033 10.000000
## 3 Angola Africa 5899 8.000000
## 4 Argentina S. America 14333 18.000000
## 5 Armenia C&E Europe 6070 15.000000
## 6 Australia Asia-Pacific 35677 20.000000
## 7 Austria W. Europe 38152 20.000000
## 8 Azerbaijan C&E Europe 8765 3.000000
## 9 Bahrain Middle East 34605 3.000000
## 10 Bangladesh Asia-Pacific 1334 16.000000
## 11 Belarus C&E Europe 12261 3.000000
## 12 Belgium W. Europe 34493 20.000000
## 13 Benin Africa 1468 16.200000
## 14 Bhutan Asia-Pacific 4755 2.000000
## 15 Bolivia S. America 4278 18.200000
## 16 Botswana Africa 13392 19.000000
## 17 Brazil S. America 10296 18.000000
## 18 Bulgaria C&E Europe 12393 19.000000
## 19 Burkina Faso Africa 1161 10.000000
## 20 Cambodia Asia-Pacific 1905 12.000000
## 21 Cameroon Africa 2215 6.000000
## 22 Canada N. America 36444 20.000000
## 23 Central African Republic Africa 736 10.200000
## 24 Chad Africa 1455 8.000000
## 25 Chile S. America 14465 19.200000
## 26 China Asia-Pacific 5962 3.000000
## 27 Colombia S. America 8885 17.000000
## 28 Comoros Africa 1169 15.800000
## 29 Congo Brazzaville Africa 3946 6.000000
## 30 Congo Kinshasa Africa 321 15.000000
## 31 Costa Rica S. America 11241 20.000000
## 32 Croatia C&E Europe 19084 18.400000
## 33 Cuba S. America 9500 3.000000
## 34 Czech Republic C&E Europe 24712 20.000000
## 35 Denmark Scandinavia 36607 20.000000
## 36 Djibouti Africa 2140 12.000000
## 37 Ecuador S. America 8009 16.200000
## 38 Egypt Middle East 5416 4.000000
## 39 El Salvador S. America 6794 17.000000
## 40 Equatorial Guinea Africa 33873 5.000000
## 41 Eritrea Africa 632 3.000000
## 42 Estonia C&E Europe 20662 16.000000
## 43 Fiji Asia-Pacific 4382 13.800000
## 44 Finland Scandinavia 35427 20.000000
## 45 France W. Europe 34045 19.000000
## 46 Gabon Africa 14527 6.000000
## 47 Gambia Africa 1363 5.000000
## 48 Georgia C&E Europe 4896 15.666667
## 49 Germany W. Europe 35613 20.000000
## 50 Ghana Africa 1452 16.666667
## 51 Greece W. Europe 29361 20.000000
## 52 Guatemala S. America 4760 18.000000
## 53 Guinea Africa 1204 9.000000
## 54 Guinea-Bissau Africa 538 11.000000
## 55 Guyana S. America 2542 16.000000
## 56 Haiti S. America 1177 8.000000
## 57 Honduras S. America 3965 17.000000
## 58 Hungary C&E Europe 19330 20.000000
## 59 India Asia-Pacific 2972 19.000000
## 60 Indonesia Asia-Pacific 3975 16.666667
## 61 Iran Middle East 11666 10.000000
## 62 Iraq Middle East 3570 1.000000
## 63 Ireland W. Europe 44200 20.000000
## 64 Israel Middle East 27548 20.000000
## 65 Italy W. Europe 30756 20.000000
## 66 Jamaica S. America 7705 19.000000
## 67 Japan Asia-Pacific 34099 20.000000
## 68 Jordan Middle East 5283 8.000000
## 69 Kazakhstan C&E Europe 11315 4.000000
## 70 Kenya Africa 1590 18.000000
## 71 Korea South Asia-Pacific 27939 18.000000
## 72 Kuwait Middle East 39914 3.000000
## 73 Kyrgyzstan C&E Europe 2188 7.000000
## 74 Laos Asia-Pacific 2134 3.000000
## 75 Latvia C&E Europe 17100 18.000000
## 76 Lesotho Africa 1588 18.000000
## 77 Liberia Africa 388 10.000000
## 78 Libya Middle East 15402 3.000000
## 79 Lithuania C&E Europe 18824 20.000000
## 80 Macedonia C&E Europe 10041 19.000000
## 81 Madagascar Africa 1049 17.000000
## 82 Malawi Africa 837 15.000000
## 83 Malaysia Asia-Pacific 14215 13.000000
## 84 Mali Africa 1128 16.000000
## 85 Mauritania Africa 2052 4.000000
## 86 Mauritius Africa 12079 20.000000
## 87 Mexico N. America 14495 18.000000
## 88 Moldova C&E Europe 2925 18.000000
## 89 Mongolia Asia-Pacific 3566 20.000000
## 90 Morocco Middle East 4388 4.000000
## 91 Mozambique Africa 855 16.000000
## 92 Namibia Africa 6343 16.000000
## 93 Nepal Asia-Pacific 1112 4.000000
## 94 Netherlands W. Europe 40849 20.000000
## 95 New Zealand Asia-Pacific 27029 20.000000
## 96 Nicaragua S. America 2682 18.000000
## 97 Niger Africa 684 15.333333
## 98 Nigeria Africa 2082 14.000000
## 99 Norway Scandinavia 58138 20.000000
## 100 Oman Middle East 22478 2.000000
## 101 Pakistan Asia-Pacific 2644 5.000000
## 102 Papua New Guinea Asia-Pacific 2208 20.000000
## 103 Paraguay S. America 4709 17.666667
## 104 Peru S. America 8507 19.000000
## 105 Philippines Asia-Pacific 3510 18.000000
## 106 Poland C&E Europe 17625 20.000000
## 107 Portugal W. Europe 23074 20.000000
## 108 Qatar Middle East 85868 0.000000
## 109 Romania C&E Europe 14065 18.333333
## 110 Russia C&E Europe 16139 17.000000
## 111 Rwanda Africa 1022 6.666667
## 112 Saudi Arabia Middle East 23920 0.000000
## 113 Senegal Africa 1772 18.000000
## 114 Sierra Leone Africa 766 15.000000
## 115 Singapore Asia-Pacific 49284 8.000000
## 116 Slovakia C&E Europe 22081 19.000000
## 117 Slovenia C&E Europe 27605 20.000000
## 118 Solomon Islands Asia-Pacific 2610 18.000000
## 119 South Africa Africa 10109 19.000000
## 120 Spain W. Europe 31954 20.000000
## 121 Sri Lanka Asia-Pacific 4560 15.333333
## 122 Sudan Africa 2153 4.000000
## 123 Swaziland Africa 4928 1.000000
## 124 Sweden Scandinavia 37383 20.000000
## 125 Switzerland W. Europe 42536 20.000000
## 126 Taiwan Asia-Pacific 30881 19.333333
## 127 Tajikistan C&E Europe 1906 7.666667
## 128 Tanzania Africa 1263 11.000000
## 129 Thailand Asia-Pacific 7703 19.000000
## 130 Togo Africa 829 8.000000
## 131 Tunisia Middle East 7996 6.000000
## 132 Turkey Middle East 13920 17.000000
## 133 Turkmenistan C&E Europe 6641 1.000000
## 134 UAE Middle East 38830 2.000000
## 135 Uganda Africa 1165 6.000000
## 136 Ukraine C&E Europe 7271 16.000000
## 137 United Kingdom W. Europe 35445 20.000000
## 138 United States N. America 46716 20.000000
## 139 Uruguay S. America 12734 20.000000
## 140 Uzbekistan C&E Europe 2656 1.000000
## 141 Venezuela S. America 12804 16.000000
## 142 Vietnam Asia-Pacific 2785 3.000000
## 143 Yemen Middle East 2400 8.000000
## 144 Zambia Africa 1356 15.000000
## 145 Zimbabwe Africa 188 6.000000
africa <- filter(world, region == c("Africa"))
mean(africa[ ,3])
## [1] 3613.095
mean(africa[ ,4])
## [1] 11.44921
9 What was the poorest country in the world in 2008? Richest?
poorest <- world[world$gdppcap08 == min(world$gdppcap08),1]
poorest
## [1] "Zimbabwe"
richest <- world[world$gdppcap08 == max(world$gdppcap08),1]
richest
## [1] "Qatar"
10 How many countries in Europe are "rich" according to your coding? How many are poor? What percentage have Polity IV scores of at least 18?
europe_1
## country gdppcap08 polityIV status
## 1 Norway 58138 20.000000 Rich
## 2 Switzerland 42536 20.000000 Rich
## 3 United Kingdom 35445 20.000000 Rich
## 4 Italy 30756 20.000000 Rich
## 5 Czech Republic 24712 20.000000 Rich
## 6 Portugal 23074 20.000000 Rich
## 7 Hungary 19330 20.000000 Rich
## 8 Lithuania 18824 20.000000 Rich
## 9 Poland 17625 20.000000 Rich
## 10 Romania 14065 18.333333 Rich
## 11 Moldova 2925 18.000000 Poor
## 12 Albania 7715 17.800000 Poor
## 13 Ukraine 7271 16.000000 Poor
## 14 Tajikistan 1906 7.666667 Poor
## 15 Kyrgyzstan 2188 7.000000 Poor
## 16 Turkmenistan 6641 1.000000 Poor
table(europe_1["status"])
##
## Poor Rich
## 6 10