First we load the csv data into R from Github. We also load the required packages.
library(RCurl)
## Loading required package: bitops
library(tidyr)
## Warning: package 'tidyr' was built under R version 3.2.3
##
## Attaching package: 'tidyr'
##
## The following object is masked from 'package:RCurl':
##
## complete
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.2.2
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
url = 'https://raw.githubusercontent.com/cyadusha/countries/master/ctyseasonal.csv'
x = getURL(url)
countries = read.csv(file = textConnection(x), header = TRUE)
Because we are only interested in the imports and exports for India and Canada, we subset the entire dataset where the country names are either India or Canada. We also select the columns which are titled “year” and the columns which are titled from BJAN to BDEC. These are the months of the year. We do not need the code for each country. Therefore we do not select the column that has country codes.
countries = subset(countries, countries$CTYNAME == 'India'| countries$CTYNAME == 'Canada', select = c(CTYNAME, year, BJAN:BDEC))
We name the columns as follows.
colnames(countries) = c("Country", "Year", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
Now, we gather all of the months into one column. All of the numeric values below the month names are the amount imported by the country. Negative values indicate export.
countries = countries %>%
gather(Country, Year, January:December, na.rm = F)
## Warning: attributes are not identical across measure variables; they will
## be dropped
We name the columns of our new dataset as follows.
colnames(countries) = c("Country", "Year", "Month", "Import")
Now, the imports are arranged by country and year. There is no need to arrange it by month. Otherwise each month will be ordered in alphabetical order.
countries %>% arrange(Country, Year)
## Country Year Month Import
## 1 Canada 2009 January -1,791
## 2 Canada 2009 February -1,893
## 3 Canada 2009 March -1,384
## 4 Canada 2009 April -1,092
## 5 Canada 2009 May -816
## 6 Canada 2009 June -1,854
## 7 Canada 2009 July -2,323
## 8 Canada 2009 August -1,516
## 9 Canada 2009 September -1,787
## 10 Canada 2009 October -2,647
## 11 Canada 2009 November -2,023
## 12 Canada 2009 December -2,465
## 13 Canada 2010 January -3,128
## 14 Canada 2010 February -2,830
## 15 Canada 2010 March -2,536
## 16 Canada 2010 April -2,653
## 17 Canada 2010 May -2,885
## 18 Canada 2010 June -2,644
## 19 Canada 2010 July -1,625
## 20 Canada 2010 August -2,318
## 21 Canada 2010 September -1,468
## 22 Canada 2010 October -1,543
## 23 Canada 2010 November -2,139
## 24 Canada 2010 December -2,610
## 25 Canada 2011 January -2,420
## 26 Canada 2011 February -2,830
## 27 Canada 2011 March -3,046
## 28 Canada 2011 April -1,971
## 29 Canada 2011 May -3,109
## 30 Canada 2011 June -2,976
## 31 Canada 2011 July -3,327
## 32 Canada 2011 August -2,387
## 33 Canada 2011 September -3,499
## 34 Canada 2011 October -2,474
## 35 Canada 2011 November -3,290
## 36 Canada 2011 December -2,705
## 37 Canada 2012 January -2,761
## 38 Canada 2012 February -2,684
## 39 Canada 2012 March -3,088
## 40 Canada 2012 April -3,183
## 41 Canada 2012 May -2,275
## 42 Canada 2012 June -2,030
## 43 Canada 2012 July -2,393
## 44 Canada 2012 August -2,549
## 45 Canada 2012 September -1,741
## 46 Canada 2012 October -2,533
## 47 Canada 2012 November -3,765
## 48 Canada 2012 December -2,610
## 49 Canada 2013 January -2,791
## 50 Canada 2013 February -2,720
## 51 Canada 2013 March -1,986
## 52 Canada 2013 April -2,825
## 53 Canada 2013 May -2,594
## 54 Canada 2013 June -2,341
## 55 Canada 2013 July -2,913
## 56 Canada 2013 August -2,771
## 57 Canada 2013 September -3,034
## 58 Canada 2013 October -3,179
## 59 Canada 2013 November -2,326
## 60 Canada 2013 December -2,322
## 61 Canada 2014 January -3,015
## 62 Canada 2014 February -2,646
## 63 Canada 2014 March -3,185
## 64 Canada 2014 April -2,407
## 65 Canada 2014 May -3,246
## 66 Canada 2014 June -3,212
## 67 Canada 2014 July -3,225
## 68 Canada 2014 August -2,944
## 69 Canada 2014 September -3,610
## 70 Canada 2014 October -2,796
## 71 Canada 2014 November -2,034
## 72 Canada 2014 December -3,057
## 73 Canada 2015 January -859
## 74 Canada 2015 February -1,422
## 75 Canada 2015 March -808
## 76 Canada 2015 April -114
## 77 Canada 2015 May 161
## 78 Canada 2015 June -3,098
## 79 Canada 2015 July -1,976
## 80 Canada 2015 August -2,165
## 81 Canada 2015 September -1,845
## 82 Canada 2015 October -387
## 83 Canada 2015 November -977
## 84 Canada 2015 December -1,374
## 85 India 2009 January -527
## 86 India 2009 February -537
## 87 India 2009 March -463
## 88 India 2009 April -338
## 89 India 2009 May -244
## 90 India 2009 June -252
## 91 India 2009 July -257
## 92 India 2009 August -22
## 93 India 2009 September -417
## 94 India 2009 October -430
## 95 India 2009 November -613
## 96 India 2009 December -624
## 97 India 2010 January -686
## 98 India 2010 February -797
## 99 India 2010 March -788
## 100 India 2010 April -711
## 101 India 2010 May -901
## 102 India 2010 June -952
## 103 India 2010 July -898
## 104 India 2010 August -966
## 105 India 2010 September -867
## 106 India 2010 October -966
## 107 India 2010 November -861
## 108 India 2010 December -892
## 109 India 2011 January -1,035
## 110 India 2011 February -882
## 111 India 2011 March -1,142
## 112 India 2011 April -1,245
## 113 India 2011 May -1,297
## 114 India 2011 June -1,190
## 115 India 2011 July -1,392
## 116 India 2011 August -1,400
## 117 India 2011 September -1,332
## 118 India 2011 October -1,475
## 119 India 2011 November -948
## 120 India 2011 December -1,273
## 121 India 2012 January -1,540
## 122 India 2012 February -1,753
## 123 India 2012 March -1,441
## 124 India 2012 April -1,127
## 125 India 2012 May -1,468
## 126 India 2012 June -1,940
## 127 India 2012 July -2,161
## 128 India 2012 August -1,703
## 129 India 2012 September -1,242
## 130 India 2012 October -1,014
## 131 India 2012 November -1,812
## 132 India 2012 December -1,205
## 133 India 2013 January -1,152
## 134 India 2013 February -1,524
## 135 India 2013 March -1,720
## 136 India 2013 April -1,887
## 137 India 2013 May -1,852
## 138 India 2013 June -1,386
## 139 India 2013 July -1,856
## 140 India 2013 August -1,571
## 141 India 2013 September -1,702
## 142 India 2013 October -1,744
## 143 India 2013 November -1,564
## 144 India 2013 December -2,039
## 145 India 2014 January -1,883
## 146 India 2014 February -1,989
## 147 India 2014 March -1,990
## 148 India 2014 April -2,486
## 149 India 2014 May -2,071
## 150 India 2014 June -1,447
## 151 India 2014 July -1,774
## 152 India 2014 August -2,047
## 153 India 2014 September -2,075
## 154 India 2014 October -1,999
## 155 India 2014 November -1,782
## 156 India 2014 December -2,092
## 157 India 2015 January -2,040
## 158 India 2015 February -1,986
## 159 India 2015 March -1,955
## 160 India 2015 April -1,529
## 161 India 2015 May -2,026
## 162 India 2015 June -1,630
## 163 India 2015 July -1,996
## 164 India 2015 August -1,938
## 165 India 2015 September -1,984
## 166 India 2015 October -2,030
## 167 India 2015 November -2,127
## 168 India 2015 December -1,970
Now, we subset our new data set two times. One subset is for performing monthly analysis only for Canada. The other subset is for performing monthly analysis only for India. We arrange the rows in each subset by year.
cCanada = filter(countries, Country == 'Canada') %>% arrange(Year)
cIndia = filter(countries, Country == 'India') %>% arrange(Year)
Now, we load the ggplot2 package and generate a scatterplot for each country. This scatterplot plots the amount imported over each month.
library(ggplot2)
yearmonth = seq(1,84,1)
qplot(yearmonth, cIndia$Import, xlab = "Month", ylab = "Import", main = "India's Imports")
qplot(yearmonth, cCanada$Import, xlab = "Month", ylab = "Import", main = "Canada's Imports")
According to the scatterplot for India, from January 2009 to about October 2010, India seems to import more goods and export less. Then, from November 2010 onwards, India seems to export more goods and import almost no goods.
The scatterplot for Canada shows an almost uniform distribution. The export seems to fluctuate between high and low over the time period examined.
However, for both cases, the amount of imports is low. Therefore a favorable balance of trade is maintained for both countries. Import less and export more.