For today’s code challenge, we’ll use the data fertilizer_panel which is available on the course website.

The variable avfert contains the tons of fertilizer used per hectare (about 2.5 acres) of farmland across countries and across years.

For this problem, you need to calculate the median amount of fertilizer separately for each country starting in 1980 to the present. For example, fertilizer usage for Mexico that is available in the data is

year avfert
1965 0.015
1970 0.026
1975 0.046
1980 0.057
1985 0.074
1990 0.072
1995 0.061
2000 0.074

Rules:

• You cannot load any external packages (besides ggplot2 and dplyr), but any base R functions are allowed

To win

• You must email me your code brantly.callaway@uga.edu

• I’ll run exactly the code that you send me, and if you calculate the correct median amount of fertilizer for all countries, then you win.

Solution below…

library(dplyr)

data <- subset(fertilizer_panel, year >= 1980)
med_fert_by_country <- data %>% group_by(country) %>%
summarize(median_avfert = median(avfert)) %>%
as.data.frame()
med_fert_by_country
##                 country median_avfert
## 1               Algeria  0.0156501550
## 2             Argentina  0.0061351010
## 4                 Benin  0.0062558735
## 5               Bolivia  0.0028469483
## 6              Botswana  0.0032338309
## 7                Brazil  0.0777190179
## 8               Burundi  0.0031678486
## 9              Cameroon  0.0056212265
## 10                Chile  0.1081880629
## 11                China  0.2221987545
## 12             Colombia  0.1770450771
## 13           Costa Rica  0.4256410301
## 14        Cote d'Ivoire  0.0223179981
## 15   Dominican Republic  0.0859113559
## 17     Egypt, Arab Rep.  0.3848614991
## 19                 Fiji  0.1264293343
## 20                Gabon  0.0020124682
## 21          Gambia, The  0.0064138998
## 22                Ghana  0.0040090578
## 23            Guatemala  0.0998974368
## 24                Haiti  0.0043162392
## 25             Honduras  0.0193095859
## 26                India  0.0738830641
## 27            Indonesia  0.1226644292
## 28   Iran, Islamic Rep.  0.0613351390
## 29              Jamaica  0.1408390701
## 30               Jordan  0.0642600730
## 31                Kenya  0.0258333348
## 32          Korea, Rep.  0.4545066357
## 33              Lesotho  0.0160962436
## 34               Malawi  0.0210565981
## 35             Malaysia  0.5386134386
## 36                 Mali  0.0077816672
## 37           Mauritania  0.0056532356
## 38            Mauritius  0.2866933346
## 39               Mexico  0.0716425031
## 40              Morocco  0.0357644893
## 41           Mozambique  0.0020205642
## 42              Namibia  0.0000000000
## 43                Nepal  0.0320219807
## 44            Nicaragua  0.0281356424
## 45                Niger  0.0003151741
## 46             Pakistan  0.0920683444
## 47               Panama  0.0656088963
## 48             Paraguay  0.0091811260
## 49                 Peru  0.0380968302
## 50          Philippines  0.0955030993
## 51              Romania  0.1077129170
## 52               Rwanda  0.0003265846
## 53              Senegal  0.0093252277
## 54         Sierra Leone  0.0057628029
## 55         South Africa  0.0573089123
## 56            Sri Lanka  0.2269737720
## 57 Syrian Arab Republic  0.0580627061
## 58             Tanzania  0.0109782023
## 59             Thailand  0.0537285693
## 60                 Togo  0.0057220440
## 61  Trinidad and Tobago  0.0846990719
## 62              Tunisia  0.0314634331
## 63               Turkey  0.0677941293
## 64               Uganda  0.0001161479
## 65              Uruguay  0.0585582033
## 66        Venezuela, RB  0.1095280051
## 67               Zambia  0.0127835721
## 68             Zimbabwe  0.0562351495