QUESTION: The data contains information on Ghana from 1964 to 2019. There are three variables: country, inflation and gdp per capita. There is only one country in the data. That is Ghana. Here is the task: 1. Find the following: a. the year in which Ghana recorded the lowest inflation
The year in which Ghana recorded the lowest GDP per capita
What was the inflation rate in that year?
What was the GDP per capita in that year?
the year in which Ghana recorded the HIGHEST inflation
The year in which Ghana recorded the HIGHEST GDP per capita
What was the inflation rate in that year?
What was the GDP per capita in that year?
The mean inflation rate.
Median inflation rate
The mean GDP per capita
Median GDP per capita
What was the inflation rate in the year in which Ghana had the HIGHEST GDP PER CAPITA
What was the inflation rate in the year in which Ghana had the LOWEST GDP PER CAPITA
Do this in R markdown. Copy the questions into R markdown. Write your code and make sure it runs in R markdown.
#SOLUTION
data <- read.csv("Task_1_ayub.csv")
print(data)
country year gdp_per_capita inflation
1 ghana 1964 1070.7608 9.934816
2 ghana 1965 1055.2667 17.006595
3 ghana 1966 984.6391 8.152515
4 ghana 1967 991.0360 -3.878357
5 ghana 1968 972.1046 12.616517
6 ghana 1969 1006.5133 11.036817
7 ghana 1970 1077.1393 2.889154
8 ghana 1971 1103.2965 5.182013
9 ghana 1972 1045.9645 15.472559
10 ghana 1973 1046.2192 20.882492
11 ghana 1974 1088.6083 24.568734
12 ghana 1975 930.6672 29.463524
13 ghana 1976 879.0437 28.048650
14 ghana 1977 882.0585 67.250782
15 ghana 1978 939.2204 73.306695
16 ghana 1979 897.3768 37.949478
17 ghana 1980 881.1572 51.126135
18 ghana 1981 828.3981 75.633566
19 ghana 1982 749.3490 27.890579
20 ghana 1983 693.9491 123.061214
21 ghana 1984 731.3452 35.312424
22 ghana 1985 745.8590 20.648415
23 ghana 1986 761.9161 41.705796
24 ghana 1987 775.6373 39.201497
25 ghana 1988 796.0868 33.402854
26 ghana 1989 812.8900 28.294311
27 ghana 1990 816.0776 31.166587
28 ghana 1991 834.6563 20.041359
29 ghana 1992 842.3340 11.150083
30 ghana 1993 858.3242 31.757212
31 ghana 1994 862.2948 30.128927
32 ghana 1995 873.8857 43.045331
33 ghana 1996 890.6316 39.837743
34 ghana 1997 904.8696 19.458168
35 ghana 1998 924.2722 17.048465
36 ghana 1999 941.5914 13.971165
37 ghana 2000 952.8033 27.230114
38 ghana 2001 966.9376 34.817944
39 ghana 2002 986.0223 22.818585
40 ghana 2003 1012.1049 28.704407
41 ghana 2004 1042.5548 14.350151
42 ghana 2005 1076.6181 14.963718
43 ghana 2006 1116.6300 80.754581
44 ghana 2007 1135.4911 18.627888
45 ghana 2008 1207.8345 19.410271
46 ghana 2009 1234.5403 15.666569
47 ghana 2010 1299.3449 16.595645
48 ghana 2011 1446.3716 13.914822
49 ghana 2012 1543.7640 15.205279
50 ghana 2013 1618.5978 52.988982
51 ghana 2014 1627.7603 22.164000
52 ghana 2015 1625.9058 13.588402
53 ghana 2016 1644.5980 15.249381
54 ghana 2017 1739.4675 10.352752
55 ghana 2018 1808.3272 10.210203
56 ghana 2019 1884.2849 9.188794
The code below uses the functions is.data.frame to check if the data is a data.frame and not a vector
is.data.frame(data)
[1] TRUE
We will first look at the first n rows of GDP Per Capita present in the data from 1964 - 2019 using the ’head()’function
head(data$gdp_per_capita)
[1] 1070.7608 1055.2667 984.6391 991.0360 972.1046 1006.5133
Now let’s look at the first n rows of inflation rate present in the data from 1964 - 2019 using the ’head()’function
head(data$inflation)
[1] 9.934816 17.006595 8.152515 -3.878357 12.616517 11.036817
We normally required to determine the minimum and maximum values in the data analysis, these help us to understand the limits of a variable. we can do this by using the which.max to find maximum values and which.min to find minimum values. With a single square brackets we can extract rows.
Now we can answer question #1
What was the year in which Ghana recorded the lowest inflation?
data$year[which.min(data$inflation)]
[1] 1967
What was the inflation rate in that year?
data[which.min(data$inflation),]
country year gdp_per_capita inflation
4 ghana 1967 991.036 -3.878357
What was the year in which Ghana recorded the lowest GDP per capita?
data$year[which.min(data$gdp_per_capita)]
[1] 1983
What was the GDP per capita in that year?
data[which.min(data$gdp_per_capita),]
country year gdp_per_capita inflation
20 ghana 1983 693.9491 123.0612
What was the year in which Ghana recorded the HIGHEST inflation?
data$year[which.max(data$inflation)]
[1] 1983
What was the inflation rate in that year?
data[which.max(data$inflation),]
country year gdp_per_capita inflation
20 ghana 1983 693.9491 123.0612
What was the year in which Ghana recorded the HIGHEST GDP per capita?
data$year[which.max(data$gdp_per_capita)]
[1] 2019
What was the GDP per capita in that year?
data[which.max(data$gdp_per_capita),]
country year gdp_per_capita inflation
56 ghana 2019 1884.285 9.188794
mean(data$inflation)
[1] 27.6887
median(data$inflation)
[1] 20.76545
mean(data$gdp_per_capita)
[1] 1061.882
median(data$gdp_per_capita)
[1] 978.3719
What was the inflation rate in the year in which Ghana had the HIGHEST GDP PER CAPITA
data$inflation[which.max(data$gdp_per_capita)]
[1] 9.188794
What was the inflation rate in the year in which Ghana had the LOWEST GDP PER CAPITA
data$inflation[which.min(data$gdp_per_capita)]
[1] 123.0612
the relationship between inflation rate and gdp per capita over the years The codes below will create a scatterplot for the relation between GDP Per Capita and the Inflation Rate
plot(x = data$gdp_per_cap,y = data$inflation,
xlab = "GDP PER CAPITA",
ylab = "INFLATION",
xlim = c(700,2000),
ylim = c(12,124),
main = "GDP PER CAPITA vs INFLATION"
)