6.4 Data challenge: African elephants
Our World in Data is an open-access project that publishes many datasets and charts about global challenges, for example energy, food, war, technological change etc. In this data challenge, we will analyse the data set on African elephant populations.
Load the data set into R using the command
elephants <- read.csv(url("https://runifr.netlify.app/web/african-elephants.csv"))
Check the dimensions of the dataset: How many rows and columns are there?
Identify the earliest and latest year in which data was collected
How many times does the entity
Central African Republic
occur in this dataset?Create a boolean vector indicating whether or not the country code (
Code
) is missing.How many different entities are there?
Rename the column
African.Elephant.population..AfESG..2019.
(weird name) tocounts
.Sort the column
counts
from smallest to largest. Which entities and years correspond to the minimum and maximum number of elephants?Add an extra column to the data frame that indicates whether the entity is a country or a region (
Africa
,Central Africa
,Eastern Africa
,Southern Africa
,Western Africa
).Calculate the median number of elephants across all entities for the year 2007.
Extract the rows where the entity is
Eastern Africa
and where the number of African elephants is smaller than 100000.Extract the rows that correspond to the entity
Africa
and store them in a new data frameafricanElephants
. Add a new column to this data frame that calculates the fraction of elephants relative to the (estimated) number of elephants of the year 1500.The equator traverses the countries with the codes GAB, COD, UGA and KEN. Create two new entities named
Equatorial counties
with codeNA
for the years 2007 and 2015 and append them to the data frame. The number of African elephants per year is given by the sum of all elephants from these countries.