Main goal of this exercise is to try subsetting and arranging observations and variables in the trade data in R. For doing that, use R package dplyr
and data transformation related functions particularly commands:
Following the previous example, use the same working directory and download this data file to the folder 02-filter-ct-data
within your working directory. This data file contains a compress subset of the WTO data for the years 2014-2016, for all countries available in the CT and for all most aggregated commodities listed in the Annex 1 of the WTO Agreement on Agriculture. We will be using this data in the current and other exercises.
To load this data into the R session use readr
package and the function read_rds()
.
library(tidyverse)
library(readr)
ctData <- read_rds("02-filter-ct-data/subsetting-data.rds")
Practicing with the dplyr filter()
function.
Here you may need to use datasets available in the tradeAnalysis
package together with the dplyr
function for data filtering in order to generate the results that are.
Load tradeAnalysis
package and explore built in datasets. Those are:
wtoAgFood
wtoAgFoodFull
What are the difference between the previous one?classes
reporters
partners
regimes
units
If any of these datasets is not working try to access to the dataset using the namespace specification such as tradeAnalysis::units
.
library(tradeAnalysis)
tradeAnalysis::units
## # A tibble: 13 x 3
## Qty.Unit.Code Unit
## <int> <chr>
## 1 1 -
## 2 2 m2
## 3 3 1000 kWh
## 4 4 m
## 5 5 u
## 6 6 2u
## 7 7 l
## 8 8 kg
## 9 9 1000U
## 10 10 U (jeu/pack)
## 11 11 12u
## 12 12 m3
## 13 13 carat
## # ... with 1 more variables: Unit.Description <chr>
Export a timeseries for wheat and barley (HS 1001 and 1003) import and export values only in 1000 USD from all EU countries to the world for all available years. Save the time series in an object and then in the .csv
file.
To see what are the EU countries either use the full list of reporters reporters
and manually record all EU countries of subtract EU country codes from the output of the function getEU()
in the tradeAnalysis
package.
hsCodes <- c("1001", "1003")
ctSubset <-
ctData %>%
filter(
Commodity.Code %in% hsCodes,
Reporter.Code %in% getEU()$Partner.Code,
Partner.Code %in% "0"
)
Note: filtering arguments must be supplied in the for of atomic vectors only. Thus, you cannot specify hsCodesDF <- as_data_frame(list(Commodity.Code = hsCodes))
and filter with the call filter(Commodity.Code %in% hsCodesDF)
. The argument hsCodesDF
should always me either an atomic vector, or a single value or a logical argument.
The same filtering exercise could be rewritten as:
ctData %>%
mutate(Commodity.Code.Filter = ifelse(Commodity.Code %in% hsCodes, TRUE, FALSE)) %>%
filter(
Commodity.Code.Filter,
Reporter.Code %in% getEU()$Partner.Code,
Partner.Code %in% "0"
) %>%
select(-Commodity.Code.Filter)
When we are done with filtering, we can record everything in a data frame.
write_csv(ctSubset, "02-filter-ct-data/ex1-subset.csv")
Practicing with the dplyr select()
function.
In order to make results from the exercise 1 more user friendly, we need to be able to select different set of columns for presentation using dplyr::select()
and other “Select helpers” see ?everything()
.
Use the output of the previous exercise in order to create to objects tables:
One approach is to select those variables that we need:
ctSubsetQuant <-
ctSubset %>%
select(Reporter.Code, Partner.Code, Trade.Flow.Code, Year,
Commodity.Code, Netweight..kg.) %>%
mutate(Netweight..kg. = Netweight..kg. / 10^6)
ctSubsetVal <-
ctSubset %>%
select(Reporter.Code, Partner.Code, Trade.Flow.Code, Year,
Commodity.Code, Trade.Value..US..) %>%
mutate(Trade.Value..US.. = Trade.Value..US.. / 10^3)
Another approach is in removing those variables that we do not need:
ctSubsetQuant <-
ctSubset %>%
select(-Trade.Value..US.., -Classification) %>%
mutate(Netweight..kg. = Netweight..kg. / 10^6)
ctSubsetVal <-
ctSubset %>%
select(-Netweight..kg., -Classification) %>%
mutate(Trade.Value..US.. = Trade.Value..US.. / 10^3)
Sometimes it is necessary to be able to arrange data in the user-friendly way. To do so, use function arrange()
and desc()
(to revert order withing the function arrange()
). For more information see arrange().
Use the output ctSubsetQuant
, to arrange all observations in the way that you can see data in the following way:
ctSubsetQuant %>%
arrange(Reporter.Code, Partner.Code, Commodity.Code,
desc(Trade.Flow.Code), desc(Year), desc(Year))
## # A tibble: 328 x 6
## Year Trade.Flow.Code Reporter.Code Partner.Code Commodity.Code
## <int> <int> <int> <int> <chr>
## 1 2014 1 20 0 1001
## 2 2014 1 20 0 1003
## 3 2015 2 40 0 1001
## 4 2014 2 40 0 1001
## 5 2015 1 40 0 1001
## 6 2014 1 40 0 1001
## 7 2015 2 40 0 1003
## 8 2014 2 40 0 1003
## 9 2015 1 40 0 1003
## 10 2014 1 40 0 1003
## # ... with 318 more rows, and 1 more variables: Netweight..kg. <dbl>
Sometimes it is very important to see what are the unique values existing in the data. For finding this, we can use function disitnct()
from dplyr
.
Find all unique partners of French (Reporter code 251) export and import of wheat and indicate, what are the differences.
Finding all unique reporters and partners.
franceExpImpPartn <-
ctData %>%
filter(Reporter.Code == "251",
Commodity.Code == "1001") %>%
distinct(Trade.Flow.Code, Partner.Code)
Unique destinations of export, where from France does not import any wheat:
franceExpImpPartn %>%
filter(Trade.Flow.Code == 2,
!Partner.Code %in%
filter(franceExpImpPartn, Trade.Flow.Code == 1)$Partner.Code)
## # A tibble: 63 x 2
## Trade.Flow.Code Partner.Code
## <int> <int>
## 1 2 92
## 2 2 112
## 3 2 116
## 4 2 120
## 5 2 132
## 6 2 178
## 7 2 180
## 8 2 191
## 9 2 192
## 10 2 196
## # ... with 53 more rows
Unique sources of import, where France does not export any wheat:
franceExpImpPartn %>%
filter(Trade.Flow.Code == 1,
!Partner.Code %in%
filter(franceExpImpPartn, Trade.Flow.Code == 2)$Partner.Code)
## # A tibble: 5 x 2
## Trade.Flow.Code Partner.Code
## <int> <int>
## 1 1 251
## 2 1 404
## 3 1 899
## 4 1 604
## 5 1 398
Copyright © 2017 Eduard Bukin. All rights reserved.