├── Documentation ├── cache │ ├── Merge-raw_cce99827a3b4f8c0dcdbc0ea06b1b4f1.rdb │ ├── WB-download_4d2a85dcffec80255907119f1e46580d.rdb │ ├── FAOSTAT-download_e043e1fb6f04a458b224549c66a9fe1b.rdb │ ├── Merge-raw_cce99827a3b4f8c0dcdbc0ea06b1b4f1.rdx │ ├── Merge-raw_cce99827a3b4f8c0dcdbc0ea06b1b4f1.RData │ ├── WB-download_4d2a85dcffec80255907119f1e46580d.RData │ ├── WB-download_4d2a85dcffec80255907119f1e46580d.rdx │ ├── FAOSTAT-download_e043e1fb6f04a458b224549c66a9fe1b.RData │ ├── FAOSTAT-download_e043e1fb6f04a458b224549c66a9fe1b.rdx │ └── __packages ├── .pdf ├── FAOSTAT.dvi ├── FAOSTAT.pdf └── jsslogo.jpg ├── FAOSTAT ├── demo │ ├── 00Index │ └── FAOSTATdemo.R ├── tests │ ├── testthat.R │ └── testthat │ │ └── test_geogr.R ├── inst │ └── doc │ │ └── FAOSTAT.pdf ├── data │ ├── FAOmetaTable.RData │ ├── FAOregionProfile.RData │ └── FAOcountryProfile.RData ├── R │ ├── FAOcountryProfile.R │ ├── FAOregionProfile.R │ ├── FAOmetaTable.R │ ├── FAOSTAT-package.R │ ├── ebind.R │ ├── printLab.R │ ├── geogr.R │ ├── chConstruct.R │ ├── chgr.R │ ├── fillCountryCode.R │ ├── translateUnit.R │ ├── indConstruct.R │ ├── faostat_bulk_download.R │ ├── mergeSYB.R │ ├── scaleUnit.R │ ├── overlap.R │ ├── getWDI.R │ ├── translateCountryCode.R │ ├── CHMT.R │ ├── grConstruct.R │ ├── lsgr.R │ ├── shConstruct.R │ ├── getWDImetaData.R │ ├── FAOcheck.R │ ├── FAOsearch.R │ ├── constructSYB.R │ ├── getFAO.R │ ├── getWDItoSYB.R │ └── getFAOtoSYB.R ├── man │ ├── FAOcountryProfile.Rd │ ├── FAOregionProfile.Rd │ ├── FAOmetaTable.Rd │ ├── FAOsearch.Rd │ ├── FAOSTAT-package.Rd │ ├── printLab.Rd │ ├── ebind.Rd │ ├── fillCountryCode.Rd │ ├── translateUnit.Rd │ ├── translateCountryCode.Rd │ ├── overlap.Rd │ ├── CHMT.Rd │ ├── mergeSYB.Rd │ ├── chgr.Rd │ ├── indConstruct.Rd │ ├── geogr.Rd │ ├── getWDImetaData.Rd │ ├── chConstruct.Rd │ ├── download_faostat_bulk.Rd │ ├── read_faostat_bulk.Rd │ ├── scaleUnit.Rd │ ├── lsgr.Rd │ ├── grConstruct.Rd │ ├── getWDI.Rd │ ├── FAOcheck.Rd │ ├── shConstruct.Rd │ ├── constructSYB.Rd │ ├── getWDItoSYB.Rd │ ├── getFAO.Rd │ ├── getFAOtoSYB.Rd │ └── Aggregation.Rd ├── NAMESPACE └── DESCRIPTION ├── Codes ├── tests │ ├── testthat.R │ └── testthat │ │ └── test_geogr.R └── R │ ├── FAOcountryProfile.R │ ├── FAOregionProfile.R │ ├── FAOmetaTable.R │ ├── FAOSTAT-package.R │ ├── ebind.R │ ├── printLab.R │ ├── geogr.R │ ├── chConstruct.R │ ├── chgr.R │ ├── fillCountryCode.R │ ├── translateUnit.R │ ├── indConstruct.R │ ├── mergeSYB.R │ ├── scaleUnit.R │ ├── overlap.R │ ├── getWDI.R │ ├── translateCountryCode.R │ ├── CHMT.R │ ├── grConstruct.R │ ├── lsgr.R │ ├── shConstruct.R │ ├── getWDImetaData.R │ ├── FAOcheck.R │ ├── FAOsearch.R │ ├── constructSYB.R │ ├── getFAO.R │ ├── getWDItoSYB.R │ └── getFAOtoSYB.R ├── FAOmetaTable.RData ├── FAOcountryProfile.csv ├── FAOregionProfile.RData ├── FAOcountryProfile.RData ├── .travis.yml ├── DESCRIPTION ├── README.md ├── FAOSTATdemo.R └── FAOSTATbuild.R /Documentation/cache/Merge-raw_cce99827a3b4f8c0dcdbc0ea06b1b4f1.rdb: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Documentation/cache/WB-download_4d2a85dcffec80255907119f1e46580d.rdb: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Documentation/cache/FAOSTAT-download_e043e1fb6f04a458b224549c66a9fe1b.rdb: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /FAOSTAT/demo/00Index: -------------------------------------------------------------------------------- 1 | FAOSTATdemo Demonstration for the FAOSTAT package 2 | -------------------------------------------------------------------------------- /Codes/tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(FAOSTAT) 3 | 4 | test_check("FAOSTAT") 5 | -------------------------------------------------------------------------------- /Documentation/.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/.pdf -------------------------------------------------------------------------------- /FAOSTAT/tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(FAOSTAT) 3 | 4 | test_check("FAOSTAT") 5 | -------------------------------------------------------------------------------- /FAOmetaTable.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/FAOmetaTable.RData -------------------------------------------------------------------------------- /FAOcountryProfile.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/FAOcountryProfile.csv -------------------------------------------------------------------------------- /FAOregionProfile.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/FAOregionProfile.RData -------------------------------------------------------------------------------- /Documentation/FAOSTAT.dvi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/FAOSTAT.dvi -------------------------------------------------------------------------------- /Documentation/FAOSTAT.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/FAOSTAT.pdf -------------------------------------------------------------------------------- /Documentation/jsslogo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/jsslogo.jpg -------------------------------------------------------------------------------- /FAOcountryProfile.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/FAOcountryProfile.RData -------------------------------------------------------------------------------- /FAOSTAT/inst/doc/FAOSTAT.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/FAOSTAT/inst/doc/FAOSTAT.pdf -------------------------------------------------------------------------------- /FAOSTAT/data/FAOmetaTable.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/FAOSTAT/data/FAOmetaTable.RData -------------------------------------------------------------------------------- /FAOSTAT/data/FAOregionProfile.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/FAOSTAT/data/FAOregionProfile.RData -------------------------------------------------------------------------------- /FAOSTAT/data/FAOcountryProfile.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/FAOSTAT/data/FAOcountryProfile.RData -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: R 2 | cache: packages 3 | r: 3.2 4 | r_github_packages: 5 | - jimhester/covr 6 | after_success: 7 | - Rscript -e 'library(covr);codecov("FAOSTAT/tests")' -------------------------------------------------------------------------------- /Documentation/cache/Merge-raw_cce99827a3b4f8c0dcdbc0ea06b1b4f1.rdx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/cache/Merge-raw_cce99827a3b4f8c0dcdbc0ea06b1b4f1.rdx -------------------------------------------------------------------------------- /Documentation/cache/Merge-raw_cce99827a3b4f8c0dcdbc0ea06b1b4f1.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/cache/Merge-raw_cce99827a3b4f8c0dcdbc0ea06b1b4f1.RData -------------------------------------------------------------------------------- /Documentation/cache/WB-download_4d2a85dcffec80255907119f1e46580d.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/cache/WB-download_4d2a85dcffec80255907119f1e46580d.RData -------------------------------------------------------------------------------- /Documentation/cache/WB-download_4d2a85dcffec80255907119f1e46580d.rdx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/cache/WB-download_4d2a85dcffec80255907119f1e46580d.rdx -------------------------------------------------------------------------------- /Documentation/cache/FAOSTAT-download_e043e1fb6f04a458b224549c66a9fe1b.RData: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/cache/FAOSTAT-download_e043e1fb6f04a458b224549c66a9fe1b.RData -------------------------------------------------------------------------------- /Documentation/cache/FAOSTAT-download_e043e1fb6f04a458b224549c66a9fe1b.rdx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mkao006/FAOSTATpackage/HEAD/Documentation/cache/FAOSTAT-download_e043e1fb6f04a458b224549c66a9fe1b.rdx -------------------------------------------------------------------------------- /Codes/R/FAOcountryProfile.R: -------------------------------------------------------------------------------- 1 | ##' Country profile 2 | ##' 3 | ##' The country profile containing the codes and names of countries. 4 | ##' 5 | ##' @docType data 6 | ##' @keywords datasets 7 | ##' @name FAOcountryProfile 8 | ##' 9 | NULL 10 | 11 | -------------------------------------------------------------------------------- /FAOSTAT/R/FAOcountryProfile.R: -------------------------------------------------------------------------------- 1 | ##' Country profile 2 | ##' 3 | ##' The country profile containing the codes and names of countries. 4 | ##' 5 | ##' @docType data 6 | ##' @keywords datasets 7 | ##' @name FAOcountryProfile 8 | ##' 9 | NULL 10 | 11 | -------------------------------------------------------------------------------- /Codes/R/FAOregionProfile.R: -------------------------------------------------------------------------------- 1 | ##' Regional profile 2 | ##' 3 | ##' Region profile containing the codes, names and regional 4 | ##' classifications of countries. 5 | ##' 6 | ##' @docType data 7 | ##' @keywords datasets 8 | ##' @name FAOregionProfile 9 | ##' 10 | NULL 11 | 12 | 13 | -------------------------------------------------------------------------------- /FAOSTAT/R/FAOregionProfile.R: -------------------------------------------------------------------------------- 1 | ##' Regional profile 2 | ##' 3 | ##' Region profile containing the codes, names and regional 4 | ##' classifications of countries. 5 | ##' 6 | ##' @docType data 7 | ##' @keywords datasets 8 | ##' @name FAOregionProfile 9 | ##' 10 | NULL 11 | 12 | 13 | -------------------------------------------------------------------------------- /Codes/R/FAOmetaTable.R: -------------------------------------------------------------------------------- 1 | ##' The search tree for FAOSTAT3 2 | ##' 3 | ##' A table containing the relationship between the domain, element, 4 | ##' item codes for downloading data from the FAOSTAT API. 5 | ##' 6 | ##' @docType data 7 | ##' @keywords datasets 8 | ##' @name FAOmetaTable 9 | ##' 10 | NULL 11 | -------------------------------------------------------------------------------- /FAOSTAT/R/FAOmetaTable.R: -------------------------------------------------------------------------------- 1 | ##' The search tree for FAOSTAT3 2 | ##' 3 | ##' A table containing the relationship between the domain, element, 4 | ##' item codes for downloading data from the FAOSTAT API. 5 | ##' 6 | ##' @docType data 7 | ##' @keywords datasets 8 | ##' @name FAOmetaTable 9 | ##' 10 | NULL 11 | -------------------------------------------------------------------------------- /FAOSTAT/man/FAOcountryProfile.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/FAOcountryProfile.R 3 | \docType{data} 4 | \name{FAOcountryProfile} 5 | \alias{FAOcountryProfile} 6 | \title{Country profile} 7 | \description{ 8 | The country profile containing the codes and names of countries. 9 | } 10 | \keyword{datasets} 11 | -------------------------------------------------------------------------------- /FAOSTAT/man/FAOregionProfile.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/FAOregionProfile.R 3 | \docType{data} 4 | \name{FAOregionProfile} 5 | \alias{FAOregionProfile} 6 | \title{Regional profile} 7 | \description{ 8 | Region profile containing the codes, names and regional 9 | classifications of countries. 10 | } 11 | \keyword{datasets} 12 | -------------------------------------------------------------------------------- /FAOSTAT/man/FAOmetaTable.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/FAOmetaTable.R 3 | \docType{data} 4 | \name{FAOmetaTable} 5 | \alias{FAOmetaTable} 6 | \title{The search tree for FAOSTAT3} 7 | \description{ 8 | A table containing the relationship between the domain, element, 9 | item codes for downloading data from the FAOSTAT API. 10 | } 11 | \keyword{datasets} 12 | -------------------------------------------------------------------------------- /FAOSTAT/man/FAOsearch.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/FAOsearch.R 3 | \name{FAOsearch} 4 | \alias{FAOsearch} 5 | \title{A function to find the domain, element and item code for a 6 | specific FAOSTAT query.} 7 | \usage{ 8 | FAOsearch() 9 | } 10 | \description{ 11 | A function to find the domain, element and item code for a 12 | specific FAOSTAT query. 13 | } 14 | -------------------------------------------------------------------------------- /Documentation/cache/__packages: -------------------------------------------------------------------------------- 1 | nlme 2 | FAOSYB 3 | labeling 4 | gpclib 5 | rgdal 6 | classInt 7 | e1071 8 | class 9 | maptools 10 | lattice 11 | grid 12 | sp 13 | foreign 14 | slidify 15 | codetools 16 | FAOSTAT 17 | MASS 18 | data.table 19 | reshape2 20 | RColorBrewer 21 | plyr 22 | RJSONIO 23 | ggplot2 24 | knitr 25 | stats 26 | graphics 27 | grDevices 28 | utils 29 | datasets 30 | methods 31 | base 32 | -------------------------------------------------------------------------------- /FAOSTAT/man/FAOSTAT-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/FAOSTAT-package.R 3 | \docType{package} 4 | \name{FAOSTAT-package} 5 | \alias{FAOSTAT-package} 6 | \title{A complementary package to the FAOSTAT database and the 7 | Statistical Yearbook of the Food and Agricultural Organization of 8 | the United Nations.} 9 | \description{ 10 | A complementary package to the FAOSTAT database and the 11 | Statistical Yearbook of the Food and Agricultural Organization of 12 | the United Nations. 13 | } 14 | \author{ 15 | Michael. C. J. Kao \email{michael.kao@fao.org} 16 | } 17 | \keyword{package} 18 | -------------------------------------------------------------------------------- /FAOSTAT/man/printLab.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/printLab.R 3 | \name{printLab} 4 | \alias{printLab} 5 | \title{Print labels} 6 | \usage{ 7 | printLab(label, span = FALSE, width = getOption("width")) 8 | } 9 | \arguments{ 10 | \item{label}{The label to be printed} 11 | 12 | \item{span}{Whether the dash should span the whole width of the 13 | screen(80 characters)} 14 | 15 | \item{width}{The width of the screen.} 16 | } 17 | \value{ 18 | The formatted print 19 | } 20 | \description{ 21 | A function to print standardised formatted labels without having 22 | messy codes in the functions. 23 | } 24 | -------------------------------------------------------------------------------- /FAOSTAT/man/ebind.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ebind.R 3 | \name{ebind} 4 | \alias{ebind} 5 | \title{A function to bind the different entity level.} 6 | \usage{ 7 | ebind(territory = NULL, subregion = NULL, region = NULL, world = NULL) 8 | } 9 | \arguments{ 10 | \item{territory}{The data frame which contains the 11 | territory/country level data} 12 | 13 | \item{subregion}{The sub aggregated region aggregate} 14 | 15 | \item{region}{The macro region aggregate} 16 | 17 | \item{world}{The world aggregate} 18 | } 19 | \description{ 20 | A data frame is chosen over the list is solely for the purpose of 21 | transition to ggplot2. 22 | } 23 | -------------------------------------------------------------------------------- /FAOSTAT/man/fillCountryCode.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/fillCountryCode.R 3 | \name{fillCountryCode} 4 | \alias{fillCountryCode} 5 | \title{A function to get country code when not available in data.} 6 | \usage{ 7 | fillCountryCode(country, data, outCode = "FAOST_CODE") 8 | } 9 | \arguments{ 10 | \item{country}{The column name of the data which contains 11 | the country name} 12 | 13 | \item{data}{The data frame to be matched} 14 | 15 | \item{outCode}{The output country code system, defaulted to FAO standard.} 16 | } 17 | \description{ 18 | This function can be useful when a dataset provided does not have 19 | a country code available. 20 | } 21 | -------------------------------------------------------------------------------- /FAOSTAT/man/translateUnit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/translateUnit.R 3 | \name{translateUnit} 4 | \alias{translateUnit} 5 | \title{Function to translate multipliers} 6 | \usage{ 7 | translateUnit(vec) 8 | } 9 | \arguments{ 10 | \item{vec}{The vector containing name or number to be translated} 11 | } 12 | \description{ 13 | This function translates number to character name or vice versa 14 | } 15 | \examples{ 16 | ## Create numeric vector 17 | myUnit = c(1000, 1e6, 1000, 1e9, 1e9, 1e12) 18 | 19 | ## Translate numeric to character 20 | myUnit2 = translateUnit(myUnit) 21 | myUnit2 22 | 23 | ## Now translate back 24 | translateUnit(myUnit2) 25 | 26 | } 27 | -------------------------------------------------------------------------------- /FAOSTAT/man/translateCountryCode.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/translateCountryCode.R 3 | \name{translateCountryCode} 4 | \alias{translateCountryCode} 5 | \title{A function to translate between different country coding systems} 6 | \usage{ 7 | translateCountryCode(data, from, to, oldCode) 8 | } 9 | \arguments{ 10 | \item{data}{The data frame} 11 | 12 | \item{from}{The name of the old coding system} 13 | 14 | \item{to}{The name of the new coding system} 15 | 16 | \item{oldCode}{The column name of the old country coding scheme} 17 | } 18 | \description{ 19 | The function translate any country code scheme to another if both 20 | are in the FAOcountryProfile 21 | } 22 | -------------------------------------------------------------------------------- /Codes/R/FAOSTAT-package.R: -------------------------------------------------------------------------------- 1 | ##' A complementary package to the FAOSTAT database and the 2 | ##' Statistical Yearbook of the Food and Agricultural Organization of 3 | ##' the United Nations. 4 | ##' 5 | ##' 6 | ##' @name FAOSTAT-package 7 | ##' @docType package 8 | ##' @title A complementary package to the FAOSTAT database and the 9 | ##' Statistical Yearbook of the Food and Agricultural Organization of 10 | ##' the United Nations. 11 | ##' @author Michael. C. J. Kao \email{michael.kao@@fao.org} 12 | ##' @keywords package 13 | ##' @import RJSONIO 14 | ##' @import plyr 15 | ##' @import data.table 16 | ##' @import MASS 17 | ##' @import classInt 18 | ##' @import ggplot2 19 | ##' @import labeling 20 | ##' 21 | NULL 22 | -------------------------------------------------------------------------------- /FAOSTAT/R/FAOSTAT-package.R: -------------------------------------------------------------------------------- 1 | ##' A complementary package to the FAOSTAT database and the 2 | ##' Statistical Yearbook of the Food and Agricultural Organization of 3 | ##' the United Nations. 4 | ##' 5 | ##' 6 | ##' @name FAOSTAT-package 7 | ##' @docType package 8 | ##' @title A complementary package to the FAOSTAT database and the 9 | ##' Statistical Yearbook of the Food and Agricultural Organization of 10 | ##' the United Nations. 11 | ##' @author Michael. C. J. Kao \email{michael.kao@@fao.org} 12 | ##' @keywords package 13 | ##' @import RJSONIO 14 | ##' @import plyr 15 | ##' @import data.table 16 | ##' @import MASS 17 | ##' @import classInt 18 | ##' @import ggplot2 19 | ##' @import labeling 20 | ##' 21 | NULL 22 | -------------------------------------------------------------------------------- /FAOSTAT/man/overlap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/overlap.R 3 | \name{overlap} 4 | \alias{overlap} 5 | \title{This function checks whether there are overlapping between the 6 | transitional countries.} 7 | \usage{ 8 | overlap(old, new, var, year = "Year", data, take) 9 | } 10 | \arguments{ 11 | \item{old}{The FAOST_CODE of the old countries} 12 | 13 | \item{new}{The FAOST_CODE of the new countries} 14 | 15 | \item{var}{The variable to be checked} 16 | 17 | \item{year}{The column which index the time.} 18 | 19 | \item{data}{The data frame} 20 | 21 | \item{take}{The type of check/replacement to be done.} 22 | } 23 | \description{ 24 | This function checks whether there are overlapping between the 25 | transitional countries. 26 | } 27 | -------------------------------------------------------------------------------- /FAOSTAT/tests/testthat/test_geogr.R: -------------------------------------------------------------------------------- 1 | context("Testing function geogr()") 2 | 3 | 4 | test_that("Function geogr() generate correct value", { 5 | expect_that(geogr(1), gives_warning()) 6 | expect_that(geogr(c(1, 1)), gives_warning()) 7 | expect_equal(geogr(rep(1, 5)), c(NA, rep(0, 4))) 8 | expect_equal(geogr(c(1, 2, 4, 8, 16)), c(NA, rep(100, 4))) 9 | expect_equal(geogr(1:5), c(NA, 1/1:4) * 100) 10 | }) 11 | 12 | 13 | test_that("Function geogr() gives correct output", { 14 | ## Testing class 15 | expect_that(geogr(1:5), is_a("numeric")) 16 | 17 | ## Testing whether the right number of NA is filled 18 | expect_equal(length(na.omit(geogr(1:5))), 4) 19 | expect_equal(length(na.omit(geogr(1:5, 2))), 3) 20 | expect_equal(length(na.omit(geogr(1:10, 5))), 5) 21 | }) 22 | -------------------------------------------------------------------------------- /FAOSTAT/NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(Aggregation) 4 | export(CHMT) 5 | export(FAOcheck) 6 | export(FAOsearch) 7 | export(chConstruct) 8 | export(chgr) 9 | export(constructSYB) 10 | export(download_faostat_bulk) 11 | export(ebind) 12 | export(fillCountryCode) 13 | export(geogr) 14 | export(getFAO) 15 | export(getFAOtoSYB) 16 | export(getWDI) 17 | export(getWDImetaData) 18 | export(getWDItoSYB) 19 | export(grConstruct) 20 | export(indConstruct) 21 | export(lsgr) 22 | export(mergeSYB) 23 | export(overlap) 24 | export(printLab) 25 | export(read_faostat_bulk) 26 | export(scaleUnit) 27 | export(shConstruct) 28 | export(translateCountryCode) 29 | export(translateUnit) 30 | import(MASS) 31 | import(RJSONIO) 32 | import(classInt) 33 | import(data.table) 34 | import(ggplot2) 35 | import(labeling) 36 | import(plyr) 37 | -------------------------------------------------------------------------------- /Codes/tests/testthat/test_geogr.R: -------------------------------------------------------------------------------- 1 | context("Testing function geogr()") 2 | 3 | 4 | test_that("Function geogr() generate correct value", { 5 | expect_warning(geogr(1), "Time series does not have sufficient values") 6 | expect_warning(geogr(c(1, 1)), "Time series does not have sufficient values") 7 | expect_equal(geogr(rep(1, 5)), c(NA, rep(0, 4))) 8 | expect_equal(geogr(c(1, 2, 4, 8, 16)), c(NA, rep(100, 4))) 9 | expect_equal(geogr(1:5), c(NA, 1/1:4) * 100) 10 | }) 11 | 12 | 13 | test_that("Function geogr() gives correct output", { 14 | ## Testing class 15 | expect_is(geogr(1:5), "numeric") 16 | 17 | ## Testing whether the right number of NA is filled 18 | expect_equal(length(na.omit(geogr(1:5))), 4) 19 | expect_equal(length(na.omit(geogr(1:5, 2))), 3) 20 | expect_equal(length(na.omit(geogr(1:10, 5))), 5) 21 | }) 22 | -------------------------------------------------------------------------------- /FAOSTAT/man/CHMT.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/CHMT.R 3 | \name{CHMT} 4 | \alias{CHMT} 5 | \title{This function avoids double counting of China.} 6 | \usage{ 7 | CHMT(var, data, year = "Year") 8 | } 9 | \arguments{ 10 | \item{var}{The variables that require to be sanitized.} 11 | 12 | \item{data}{The data frame which contains the data} 13 | 14 | \item{year}{The column which correspond to the year.} 15 | } 16 | \description{ 17 | This function should only be used when performing aggregations. 18 | } 19 | \details{ 20 | We decide to use the smaller subsets in the regional level because 21 | weighting variable may not exist for other variables for the 22 | larger subsets. 23 | 24 | The function only work for FAOST_CODE, if the country coding 25 | system is not in FAOST_CODE then use the translateCountryCode 26 | function to translate it. 27 | } 28 | -------------------------------------------------------------------------------- /FAOSTAT/man/mergeSYB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mergeSYB.R 3 | \name{mergeSYB} 4 | \alias{mergeSYB} 5 | \title{Function for merging data from different source.} 6 | \usage{ 7 | mergeSYB(x, y, outCode = "FAOST_CODE", all = TRUE, ...) 8 | } 9 | \arguments{ 10 | \item{x}{data frames, or objects to be coerced to one.} 11 | 12 | \item{y}{data frames, or objects to be coerced to one.} 13 | 14 | \item{outCode}{The country code system to be used to join the 15 | different sources.} 16 | 17 | \item{all}{Same as the merge function, defaulted to an outer join.} 18 | 19 | \item{...}{Arguments to be passed on to the merge function.} 20 | } 21 | \description{ 22 | This function searches for supported country system and translate 23 | the data to allow for join. 24 | } 25 | \details{ 26 | The names of the data to be merged has to be the same as the 27 | FAOcountryProfile code name. 28 | } 29 | -------------------------------------------------------------------------------- /FAOSTAT/man/chgr.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/chgr.R 3 | \name{chgr} 4 | \alias{chgr} 5 | \title{Absolute change between the year} 6 | \usage{ 7 | chgr(x, n = 1) 8 | } 9 | \arguments{ 10 | \item{x}{The time series for the change to be calculated.} 11 | 12 | \item{n}{The period for the growth to be calculated over.} 13 | } 14 | \value{ 15 | The n-period change of the time series. 16 | } 17 | \description{ 18 | Function for generating the n-period absolute change 19 | } 20 | \details{ 21 | In order to ensure the change calculated is reliable, the 22 | following rule are applied. 23 | \enumerate{ 24 | \item 50\% of the data must be present. 25 | \item The length of the time series must be greater than n 26 | } 27 | Otherwise the growth will not be computed. 28 | } 29 | \examples{ 30 | test.ts = abs(rnorm(100)) 31 | chgr(test.ts, 1) 32 | chgr(test.ts, 3) 33 | chgr(test.ts, 10) 34 | } 35 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: FAOSTAT 2 | Type: Package 3 | Title: Download Data from the FAOSTAT Database of the Food and Agricultural Organization (FAO) of the United Nations 4 | Version: 2.1.0 5 | Date: 2016-02-22 6 | Author: Michael C. J. Kao , Markus Gesmann, Filippo Gheri 7 | Maintainer: Filippo Gheri 8 | Description: A list of functions to download statistics from FAOSTAT (database of the Food and Agricultural Organization of the United Nations) and WDI (database of the World Bank), and to perform some harmonization operations. 9 | URL: https://github.com/mkao006/FAOSTATpackage 10 | Imports: 11 | RJSONIO (>= 0.96-0), 12 | plyr (>= 1.7.1), 13 | data.table (>= 1.8.2), 14 | MASS (>= 7.3-22), 15 | classInt (>= 0.1-19), 16 | ggplot2 (>= 0.9.3), 17 | labeling (>= 0.1) 18 | License: GPL (>= 2) 19 | LazyData: yes 20 | ZipData: no 21 | VignetteBuilder: knitr 22 | Suggests: knitr, 23 | testthat 24 | -------------------------------------------------------------------------------- /FAOSTAT/man/indConstruct.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/indConstruct.R 3 | \name{indConstruct} 4 | \alias{indConstruct} 5 | \title{Construct indices} 6 | \usage{ 7 | indConstruct(data, origVar, newVarName = NA, baseYear = 2000) 8 | } 9 | \arguments{ 10 | \item{data}{The data frame containing the data} 11 | 12 | \item{origVar}{The variable in which the indices is to be computed} 13 | 14 | \item{newVarName}{The name assigned to the new variable, if missing 15 | then .SC/.SH/.GR/.CH/.IND will be appended depending on the type of 16 | construction.} 17 | 18 | \item{baseYear}{The year which will serve as the base} 19 | } 20 | \value{ 21 | The indice 22 | } 23 | \description{ 24 | A function for constructing indices 25 | } 26 | \examples{ 27 | test.df = data.frame(FAOST_CODE = rep(1, 100), Year = 1901:2000, 28 | test = 1:100) 29 | indConstruct(test.df, origVar = "test", baseYear = 1950) 30 | 31 | } 32 | -------------------------------------------------------------------------------- /FAOSTAT/man/geogr.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/geogr.R 3 | \name{geogr} 4 | \alias{geogr} 5 | \title{Geometric growth rate} 6 | \usage{ 7 | geogr(x, n = 1) 8 | } 9 | \arguments{ 10 | \item{x}{The time series for the growth rate to be calculated.} 11 | 12 | \item{n}{The period for the growth to be calculated over.} 13 | } 14 | \value{ 15 | The n-period geometric growth rate of the time series. 16 | } 17 | \description{ 18 | Function for generating the n-period rolling geometric growth rate. 19 | } 20 | \details{ 21 | In order to ensure the growth rate calculated is reliable, the 22 | following rule are applied. 23 | \enumerate{ 24 | \item 50\% of the data must be present. 25 | \item The length of the time series must be greater than n 26 | } 27 | Otherwise the growth will not be computed. 28 | } 29 | \examples{ 30 | test.ts = abs(rnorm(100)) 31 | geogr(test.ts, 1) 32 | geogr(test.ts, 3) 33 | geogr(test.ts, 10) 34 | } 35 | -------------------------------------------------------------------------------- /FAOSTAT/man/getWDImetaData.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getWDImetaData.R 3 | \name{getWDImetaData} 4 | \alias{getWDImetaData} 5 | \title{World Bank Indicator Metadata} 6 | \usage{ 7 | getWDImetaData( 8 | indicator, 9 | printMetaData = FALSE, 10 | saveMetaData = FALSE, 11 | saveName = "worldBankMetaData" 12 | ) 13 | } 14 | \arguments{ 15 | \item{indicator}{The World Bank official indicator name.} 16 | 17 | \item{printMetaData}{logical, print out the meta data information} 18 | 19 | \item{saveMetaData}{logical, whether meta data should be saved as a 20 | local csv file.} 21 | 22 | \item{saveName}{The name of the file for the meta data to save to.} 23 | } 24 | \description{ 25 | A function to extract the definition and the meta data from the 26 | World Bank API 27 | } 28 | \examples{ 29 | ## pop.df = getWDImetaData("SP.POP.TOTL", 30 | ## printMetaData = TRUE, saveMetaData = TRUE) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /FAOSTAT/DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: FAOSTAT 2 | Type: Package 3 | Title: Download Data from the FAOSTAT Database of the Food and Agricultural Organization (FAO) of the United Nations 4 | Version: 2.1.0 5 | Date: 2016-02-22 6 | Author: Michael C. J. Kao , Markus Gesmann, Filippo Gheri 7 | Maintainer: Filippo Gheri 8 | Description: A list of functions to download statistics from FAOSTAT (database of the Food and Agricultural Organization of the United Nations) and WDI (database of the World Bank), and to perform some harmonization operations. 9 | URL: https://github.com/mkao006/FAOSTATpackage 10 | Imports: 11 | RJSONIO (>= 0.96-0), 12 | plyr (>= 1.7.1), 13 | data.table (>= 1.8.2), 14 | MASS (>= 7.3-22), 15 | classInt (>= 0.1-19), 16 | ggplot2 (>= 0.9.3), 17 | labeling (>= 0.1) 18 | License: GPL (>= 2) 19 | LazyData: yes 20 | ZipData: no 21 | VignetteBuilder: knitr 22 | Suggests: knitr, 23 | testthat 24 | RoxygenNote: 7.0.2 25 | -------------------------------------------------------------------------------- /FAOSTAT/man/chConstruct.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/chConstruct.R 3 | \name{chConstruct} 4 | \alias{chConstruct} 5 | \title{Construct year to year change} 6 | \usage{ 7 | chConstruct( 8 | data, 9 | origVar, 10 | country = "FAOST_CODE", 11 | year = "Year", 12 | newVarName = NA, 13 | n = 1 14 | ) 15 | } 16 | \arguments{ 17 | \item{data}{The data frame containing the data} 18 | 19 | \item{origVar}{The variable in which the year to year change is to 20 | be calculated} 21 | 22 | \item{country}{The column representing the index of country.} 23 | 24 | \item{year}{The column represing the index of year.} 25 | 26 | \item{newVarName}{The name assigned to the new variable, if 27 | missing then .CH will be appended.} 28 | 29 | \item{n}{The period for the change rate to be calculated.} 30 | } 31 | \value{ 32 | A data frame containing the computed year to year change 33 | rate. 34 | } 35 | \description{ 36 | A function for constructing year to year change 37 | } 38 | -------------------------------------------------------------------------------- /FAOSTAT/man/download_faostat_bulk.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/faostat_bulk_download.R 3 | \name{download_faostat_bulk} 4 | \alias{download_faostat_bulk} 5 | \title{Download bulk data from the faostat website 6 | http://www.fao.org/faostat/en/#data} 7 | \usage{ 8 | download_faostat_bulk(url_bulk, data_folder = "data_raw") 9 | } 10 | \arguments{ 11 | \item{url_bulk}{character url of the faostat bulk zip file to download} 12 | 13 | \item{data_folder}{character path of the local folder where to download the data} 14 | } 15 | \description{ 16 | Note the files called "normalized" are in long format 17 | with a year column instead of one column for each year. 18 | The long format is preferable for data analysis. 19 | } 20 | \examples{ 21 | \dontrun{ 22 | # Load global forestry data in long format 23 | url_forestry <- "http://fenixservices.fao.org/faostat/static/bulkdownloads/Forestry_E_All_Data_(Normalized).zip" 24 | download_faostat_bulk(url_forestry) 25 | } 26 | } 27 | \author{ 28 | Paul Rougieux 29 | } 30 | -------------------------------------------------------------------------------- /Codes/R/ebind.R: -------------------------------------------------------------------------------- 1 | ##' A function to bind the different entity level. 2 | ##' 3 | ##' A data frame is chosen over the list is solely for the purpose of 4 | ##' transition to ggplot2. 5 | ##' 6 | ##' @param territory The data frame which contains the 7 | ##' territory/country level data 8 | ##' @param subregion The sub aggregated region aggregate 9 | ##' @param region The macro region aggregate 10 | ##' @param world The world aggregate 11 | ##' @export 12 | 13 | ebind = function(territory = NULL, subregion = NULL, 14 | region = NULL, world = NULL){ 15 | nTerritory = NROW(territory) 16 | nSubregion = NROW(subregion) 17 | nRegion = NROW(region) 18 | nWorld = NROW(world) 19 | tmp = rbind(territory, subregion, region, world) 20 | area.df = data.frame(tmp, Area = c(rep("Territory", nTerritory), 21 | rep("subRegion", nSubregion), 22 | rep("Region", nRegion), 23 | rep("World", nWorld)), 24 | stringsAsFactors = FALSE) 25 | area.df 26 | } 27 | -------------------------------------------------------------------------------- /FAOSTAT/R/ebind.R: -------------------------------------------------------------------------------- 1 | ##' A function to bind the different entity level. 2 | ##' 3 | ##' A data frame is chosen over the list is solely for the purpose of 4 | ##' transition to ggplot2. 5 | ##' 6 | ##' @param territory The data frame which contains the 7 | ##' territory/country level data 8 | ##' @param subregion The sub aggregated region aggregate 9 | ##' @param region The macro region aggregate 10 | ##' @param world The world aggregate 11 | ##' @export 12 | 13 | ebind = function(territory = NULL, subregion = NULL, 14 | region = NULL, world = NULL){ 15 | nTerritory = NROW(territory) 16 | nSubregion = NROW(subregion) 17 | nRegion = NROW(region) 18 | nWorld = NROW(world) 19 | tmp = rbind(territory, subregion, region, world) 20 | area.df = data.frame(tmp, Area = c(rep("Territory", nTerritory), 21 | rep("subRegion", nSubregion), 22 | rep("Region", nRegion), 23 | rep("World", nWorld)), 24 | stringsAsFactors = FALSE) 25 | area.df 26 | } 27 | -------------------------------------------------------------------------------- /Codes/R/printLab.R: -------------------------------------------------------------------------------- 1 | ##' Print labels 2 | ##' 3 | ##' A function to print standardised formatted labels without having 4 | ##' messy codes in the functions. 5 | ##' 6 | ##' @param label The label to be printed 7 | ##' @param span Whether the dash should span the whole width of the 8 | ##' screen(80 characters) 9 | ##' @param width The width of the screen. 10 | ##' @export 11 | ##' @return The formatted print 12 | 13 | ## TODO (Michael): Need to wrap the label 14 | printLab = function(label, span = FALSE, width = getOption("width")){ 15 | nc = nchar(label) 16 | sides = (width - nc)/2 - 3 17 | if(span){ 18 | pre = paste(c("\n\n", rep("-", width), "\n"), collapse = "") 19 | post = paste(c("\n", rep("-", width), "\n\n"), collapse = "") 20 | } else { 21 | pre = paste(c("\n\n", rep(" ", sides), rep("-", nc + 6), 22 | rep(" ", sides), "\n"), collapse = "") 23 | post = paste(c("\n", rep(" ", sides), rep("-", nc + 6), 24 | rep(" ", sides), "\n\n"), collapse = "") 25 | } 26 | sandwich = paste(c(rep(" ", sides), "** ", label, " **", 27 | rep(" ", sides)), collapse = "") 28 | cat(paste(pre, sandwich, post, sep = "")) 29 | } 30 | -------------------------------------------------------------------------------- /FAOSTAT/R/printLab.R: -------------------------------------------------------------------------------- 1 | ##' Print labels 2 | ##' 3 | ##' A function to print standardised formatted labels without having 4 | ##' messy codes in the functions. 5 | ##' 6 | ##' @param label The label to be printed 7 | ##' @param span Whether the dash should span the whole width of the 8 | ##' screen(80 characters) 9 | ##' @param width The width of the screen. 10 | ##' @export 11 | ##' @return The formatted print 12 | 13 | ## TODO (Michael): Need to wrap the label 14 | printLab = function(label, span = FALSE, width = getOption("width")){ 15 | nc = nchar(label) 16 | sides = (width - nc)/2 - 3 17 | if(span){ 18 | pre = paste(c("\n\n", rep("-", width), "\n"), collapse = "") 19 | post = paste(c("\n", rep("-", width), "\n\n"), collapse = "") 20 | } else { 21 | pre = paste(c("\n\n", rep(" ", sides), rep("-", nc + 6), 22 | rep(" ", sides), "\n"), collapse = "") 23 | post = paste(c("\n", rep(" ", sides), rep("-", nc + 6), 24 | rep(" ", sides), "\n\n"), collapse = "") 25 | } 26 | sandwich = paste(c(rep(" ", sides), "** ", label, " **", 27 | rep(" ", sides)), collapse = "") 28 | cat(paste(pre, sandwich, post, sep = "")) 29 | } 30 | -------------------------------------------------------------------------------- /FAOSTAT/man/read_faostat_bulk.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/faostat_bulk_download.R 3 | \name{read_faostat_bulk} 4 | \alias{read_faostat_bulk} 5 | \title{Function to read zipped files and return a data frame 6 | Reads the main csv file within the archive. 7 | The main file has the same name as the name of the archive. 8 | Note: the zip archive might also contain metadata files about Flags and Symboles.} 9 | \usage{ 10 | read_faostat_bulk(zip_file_name) 11 | } 12 | \arguments{ 13 | \item{zip_file_name}{character name of the zip file to read} 14 | } 15 | \value{ 16 | data frame of FAOSTAT data 17 | } 18 | \description{ 19 | Function to read zipped files and return a data frame 20 | Reads the main csv file within the archive. 21 | The main file has the same name as the name of the archive. 22 | Note: the zip archive might also contain metadata files about Flags and Symboles. 23 | } 24 | \examples{ 25 | \dontrun{ 26 | # Read a file then assign it to a data frame and save it as rds 27 | forestry_e_all_data <- read_faostat_bulk("data_raw/Forestry_E_All_Data_(Normalized).zip") 28 | saveRDS(forestry_e_all_data,"data_raw/forestry_e_all_data.rds") 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /FAOSTAT/man/scaleUnit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/scaleUnit.R 3 | \name{scaleUnit} 4 | \alias{scaleUnit} 5 | \title{A function to standardize the unit} 6 | \usage{ 7 | scaleUnit(df, multiplier) 8 | } 9 | \arguments{ 10 | \item{df}{The data frame containing the data to be scale} 11 | 12 | \item{multiplier}{The named vector with the multiplier to be 13 | scaled. The name is mandatory in order for the function to identify 14 | the variable in the data frame. A data.frame can also be supplied 15 | with the first column being the name and the second being the 16 | numeric multiplier.} 17 | } 18 | \description{ 19 | The function standardize the data to the desirable unit when the 20 | multiplier vector is supplied. For example per 1000 people is 21 | scaled to per person by supplying a multiplier of 1000. 22 | } 23 | \examples{ 24 | 25 | ## Create the data frame 26 | test.df = data.frame(FAOST_CODE = 1:5, Year = 1995:1999, 27 | var1 = 1:5, var2 = 5:1) 28 | 29 | ## Create the named vector for scaling 30 | multiplier = c(1, 10) 31 | names(multiplier) = c("var1", "var2") 32 | 33 | ## Scale the data 34 | scaleUnit(test.df, multiplier = multiplier) 35 | } 36 | -------------------------------------------------------------------------------- /FAOSTAT/man/lsgr.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lsgr.R 3 | \name{lsgr} 4 | \alias{lsgr} 5 | \title{Least squares growth rate} 6 | \usage{ 7 | lsgr(x, n = 1) 8 | } 9 | \arguments{ 10 | \item{x}{The time series for the growth rate to be calculated} 11 | 12 | \item{n}{The period for the growth to be calculated over.} 13 | } 14 | \value{ 15 | The n-period least squares growth rate of the time series 16 | } 17 | \description{ 18 | Function for generating the n-period rolling least squares growth 19 | rate. 20 | } 21 | \details{ 22 | Missing values are ommited in the regression. (Will need to check 23 | this.) 24 | 25 | TODO (Michael): There is still some error associated with this 26 | function, will need to investigate further. Will need a rule for 27 | this, when the fluctuation is large and data are sufficient then 28 | take the lsgr, otherwise the geogr. 29 | 30 | In order to ensure the growth rate calculated is reliable, the 31 | following rule are applied. 32 | \enumerate{ 33 | \item 50\% of the data must be present. 34 | \item The length of the time series must be greater than n. 35 | } 36 | Otherwise the growth will not be computed. 37 | } 38 | \examples{ 39 | test.ts = abs(rnorm(100)) 40 | lsgr(test.ts, 1) 41 | lsgr(test.ts, 3) 42 | lsgr(test.ts, 10) 43 | 44 | } 45 | -------------------------------------------------------------------------------- /FAOSTAT/man/grConstruct.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/grConstruct.R 3 | \name{grConstruct} 4 | \alias{grConstruct} 5 | \title{Construct Growth rate} 6 | \usage{ 7 | grConstruct(data, origVar, newVarName = NA, type = c("geo", "ls", "ch"), n = 1) 8 | } 9 | \arguments{ 10 | \item{data}{The data frame containing the data} 11 | 12 | \item{origVar}{The variable in which the growth is to be calculated} 13 | 14 | \item{newVarName}{The name assigned to the new variable, if missing 15 | then .SC/.SH/.GR will be appended depending on the type of 16 | construction.} 17 | 18 | \item{type}{The type of growth rate, can be least squares or 19 | geometric} 20 | 21 | \item{n}{The period for the growth rate to be calculated (Refer to 22 | the lsgr or the geogr functions.)} 23 | } 24 | \value{ 25 | A data frame containing the computed growth rate. 26 | } 27 | \description{ 28 | A function for constructing growth rate variables. 29 | } 30 | \examples{ 31 | test.df2 = data.frame(FAOST_CODE = rep(c(1, 5000), each = 5), 32 | Year = rep(1990:1994, 2), 33 | a = rep(1:5, 2), b = rep(1:5, 2)) 34 | grConstruct(test.df2, origVar = "a", type = "geo", n = 1) 35 | grConstruct(test.df2, origVar = "a", type = "geo", n = 3) 36 | grConstruct(test.df2, origVar = "a", type = "geo", n = 5) 37 | } 38 | -------------------------------------------------------------------------------- /FAOSTAT/man/getWDI.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getWDI.R 3 | \name{getWDI} 4 | \alias{getWDI} 5 | \title{Access to World Bank WDI API} 6 | \usage{ 7 | getWDI( 8 | indicator = "SP.POP.TOTL", 9 | name = NULL, 10 | startDate = 1960, 11 | endDate = format(Sys.Date(), "\%Y"), 12 | printURL = FALSE, 13 | outputFormat = "wide" 14 | ) 15 | } 16 | \arguments{ 17 | \item{indicator}{The World Bank official indicator name.} 18 | 19 | \item{name}{The new name to be used in the column.} 20 | 21 | \item{startDate}{The start date for the data to begin} 22 | 23 | \item{endDate}{The end date.} 24 | 25 | \item{printURL}{Whether the url link for the data should be printed} 26 | 27 | \item{outputFormat}{The format of the data, can be 'long' or 'wide'.} 28 | } 29 | \value{ 30 | A data frame containing the desired World Bank Indicator 31 | } 32 | \description{ 33 | A function to extract data from the World Bank API 34 | } 35 | \details{ 36 | Please refer to \url{http://data.worldbank.org/node/18} for any 37 | difference between the country code system. Further details on World 38 | Bank classification and methodology are available at 39 | \url{http://data.worldbank.org/}. 40 | } 41 | \examples{ 42 | ## pop.df = getWDI() 43 | 44 | } 45 | \seealso{ 46 | \code{\link{getFAO}}, \code{\link{getWDItoSYB}}, 47 | \code{\link{getFAOtoSYB}} 48 | } 49 | -------------------------------------------------------------------------------- /Codes/R/geogr.R: -------------------------------------------------------------------------------- 1 | ##' Geometric growth rate 2 | ##' 3 | ##' Function for generating the n-period rolling geometric growth rate. 4 | ##' 5 | ##' In order to ensure the growth rate calculated is reliable, the 6 | ##' following rule are applied. 7 | ##' \enumerate{ 8 | ##' \item 50\% of the data must be present. 9 | ##' \item The length of the time series must be greater than n 10 | ##' } 11 | ##' Otherwise the growth will not be computed. 12 | ##' 13 | ##' @param x The time series for the growth rate to be calculated. 14 | ##' @param n The period for the growth to be calculated over. 15 | ##' @return The n-period geometric growth rate of the time series. 16 | ##' @export 17 | ##' @examples 18 | ##' test.ts = abs(rnorm(100)) 19 | ##' geogr(test.ts, 1) 20 | ##' geogr(test.ts, 3) 21 | ##' geogr(test.ts, 10) 22 | geogr = function(x, n = 1){ 23 | T = length(x) 24 | if(sum(is.na(x)) == T){ 25 | geogr = rep(NA, T) 26 | warning("All values are NA") 27 | } else { 28 | firstObs = ifelse(any(is.na(x)), min(which(!is.na(x))), 1) 29 | if(n > T - firstObs - 1){ 30 | geogr = rep(NA, T) 31 | warning("Time series does not have sufficient values") 32 | } else { 33 | geogr = double(T) 34 | geogr[1:(firstObs + n - 1)] = NA 35 | geogr[(firstObs + n):T] = ((x[(firstObs + n):T]/ 36 | x[firstObs:(T - n)])^(1/n) - 1) * 100 37 | } 38 | } 39 | geogr 40 | } 41 | -------------------------------------------------------------------------------- /FAOSTAT/R/geogr.R: -------------------------------------------------------------------------------- 1 | ##' Geometric growth rate 2 | ##' 3 | ##' Function for generating the n-period rolling geometric growth rate. 4 | ##' 5 | ##' In order to ensure the growth rate calculated is reliable, the 6 | ##' following rule are applied. 7 | ##' \enumerate{ 8 | ##' \item 50\% of the data must be present. 9 | ##' \item The length of the time series must be greater than n 10 | ##' } 11 | ##' Otherwise the growth will not be computed. 12 | ##' 13 | ##' @param x The time series for the growth rate to be calculated. 14 | ##' @param n The period for the growth to be calculated over. 15 | ##' @return The n-period geometric growth rate of the time series. 16 | ##' @export 17 | ##' @examples 18 | ##' test.ts = abs(rnorm(100)) 19 | ##' geogr(test.ts, 1) 20 | ##' geogr(test.ts, 3) 21 | ##' geogr(test.ts, 10) 22 | geogr = function(x, n = 1){ 23 | T = length(x) 24 | if(sum(is.na(x)) == T){ 25 | geogr = rep(NA, T) 26 | warning("All values are NA") 27 | } else { 28 | firstObs = ifelse(any(is.na(x)), min(which(!is.na(x))), 1) 29 | if(n > T - firstObs - 1){ 30 | geogr = rep(NA, T) 31 | warning("Time series does not have sufficient values") 32 | } else { 33 | geogr = double(T) 34 | geogr[1:(firstObs + n - 1)] = NA 35 | geogr[(firstObs + n):T] = ((x[(firstObs + n):T]/ 36 | x[firstObs:(T - n)])^(1/n) - 1) * 100 37 | } 38 | } 39 | geogr 40 | } 41 | -------------------------------------------------------------------------------- /FAOSTAT/man/FAOcheck.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/FAOcheck.R 3 | \name{FAOcheck} 4 | \alias{FAOcheck} 5 | \title{This function perform some check on the data} 6 | \usage{ 7 | FAOcheck( 8 | var, 9 | year = "Year", 10 | data, 11 | type = c("overlap", "multiChina"), 12 | take = c("simpleCheck", "takeNew", "takeOld", "complete") 13 | ) 14 | } 15 | \arguments{ 16 | \item{var}{The variable to be checked.} 17 | 18 | \item{year}{The column which index the time.} 19 | 20 | \item{data}{The data frame.} 21 | 22 | \item{type}{The type of check.} 23 | 24 | \item{take}{The type of check/replacement to be done in case of 25 | type equals to overlap.} 26 | } 27 | \description{ 28 | The function only works for FAOST_CODE. If the country coding 29 | system is not in FAOST_CODE then use the translateCountryCode 30 | function to translate it. 31 | } 32 | \examples{ 33 | ## test.df = 34 | ## data.frame(FAOST_CODE = rep(c(51,167,199), each = 3), 35 | ## Year = rep(c(1990:1992), 3), 36 | ## Value = c(c(3,4,4), c(2,2,2), c(1,2,NA))) 37 | ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "simpleCheck") 38 | ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "takeNew") 39 | ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "takeOld") 40 | ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "complete") 41 | } 42 | -------------------------------------------------------------------------------- /Codes/R/chConstruct.R: -------------------------------------------------------------------------------- 1 | ##' Construct year to year change 2 | ##' 3 | ##' A function for constructing year to year change 4 | ##' 5 | ##' @param origVar The variable in which the year to year change is to 6 | ##' be calculated 7 | ##' @param country The column representing the index of country. 8 | ##' @param year The column represing the index of year. 9 | ##' @param data The data frame containing the data 10 | ##' @param newVarName The name assigned to the new variable, if 11 | ##' missing then .CH will be appended. 12 | ##' @param n The period for the change rate to be calculated. 13 | ##' @return A data frame containing the computed year to year change 14 | ##' rate. 15 | ##' @export 16 | 17 | chConstruct = function(data, origVar, country = "FAOST_CODE", year = "Year", 18 | newVarName = NA, n = 1){ 19 | tmp = arrange(subset(data, select = c(country, year, origVar)), 20 | get(country), get(year)) 21 | unqCountry = unique(tmp[, country]) 22 | chVar = double() 23 | for(i in 1:length(unqCountry)){ 24 | tmpgr = chgr(as.numeric(unlist(subset(tmp, select = origVar, 25 | subset = get(country) == unqCountry[i]))), n) 26 | chVar = c(chVar, tmpgr) 27 | } 28 | ch.df = data.frame(tmp[c(country, year)], chVar) 29 | colnames(ch.df)[3] <- ifelse(is.na(newVarName), 30 | paste(origVar, ".CH", n, sep = ""), 31 | newVarName) 32 | ch.df 33 | } 34 | -------------------------------------------------------------------------------- /FAOSTAT/R/chConstruct.R: -------------------------------------------------------------------------------- 1 | ##' Construct year to year change 2 | ##' 3 | ##' A function for constructing year to year change 4 | ##' 5 | ##' @param origVar The variable in which the year to year change is to 6 | ##' be calculated 7 | ##' @param country The column representing the index of country. 8 | ##' @param year The column represing the index of year. 9 | ##' @param data The data frame containing the data 10 | ##' @param newVarName The name assigned to the new variable, if 11 | ##' missing then .CH will be appended. 12 | ##' @param n The period for the change rate to be calculated. 13 | ##' @return A data frame containing the computed year to year change 14 | ##' rate. 15 | ##' @export 16 | 17 | chConstruct = function(data, origVar, country = "FAOST_CODE", year = "Year", 18 | newVarName = NA, n = 1){ 19 | tmp = arrange(subset(data, select = c(country, year, origVar)), 20 | get(country), get(year)) 21 | unqCountry = unique(tmp[, country]) 22 | chVar = double() 23 | for(i in 1:length(unqCountry)){ 24 | tmpgr = chgr(as.numeric(unlist(subset(tmp, select = origVar, 25 | subset = get(country) == unqCountry[i]))), n) 26 | chVar = c(chVar, tmpgr) 27 | } 28 | ch.df = data.frame(tmp[c(country, year)], chVar) 29 | colnames(ch.df)[3] <- ifelse(is.na(newVarName), 30 | paste(origVar, ".CH", n, sep = ""), 31 | newVarName) 32 | ch.df 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | FAOSTAT 2 | ======= 3 | [![travis-ci-build-status](https://travis-ci.org/mkao006/FAOSTATpackage.svg?branch=master)](https://travis-ci.org/mkao006/FAOSTATpackage) 4 | [![codecov.io](https://codecov.io/github/mkao006/FAOSTATpackage/coverage.svg?branch=master)](https://codecov.io/github/mkao006/FAOSTATpackage?branch=master) 5 | [![CRAN version](http://www.r-pkg.org/badges/version/FAOSTAT)](http://cran.rstudio.com/web/packages/FAOSTAT/index.html) 6 | [![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/FAOSTAT)](http://cran.r-project.org/web/packages/FAOSTAT/index.html) 7 | 8 | This repository contains all the files to build the FAOSTAT package. 9 | 10 | # NOTE: This package has been orphaned due to the current re-development of the FAOSTAT API and the fact that I no longer work for the organisation and has no access to the updates. 11 | 12 | ============================================================================== 13 | 14 | The package can be installed from CRAN: 15 | 16 | ```r 17 | install.packages("FAOSTAT") 18 | ``` 19 | 20 | or install the develop version via the following link, different 21 | version can be installed by specifying the **ref** argument. 22 | 23 | ```r 24 | library(devtools) 25 | install_github(repo = "mkao006/FAOSTATpackage", subdir = "FAOSTAT") 26 | ``` 27 | 28 | Vignettes and demos are available and please make use of them: 29 | 30 | ```r 31 | vignette(topic = "FAOSTAT") 32 | demo(topic = "FAOSTATdemo") 33 | ``` 34 | -------------------------------------------------------------------------------- /Codes/R/chgr.R: -------------------------------------------------------------------------------- 1 | ##' Absolute change between the year 2 | ##' 3 | ##' Function for generating the n-period absolute change 4 | ##' 5 | ##' In order to ensure the change calculated is reliable, the 6 | ##' following rule are applied. 7 | ##' \enumerate{ 8 | ##' \item 50\% of the data must be present. 9 | ##' \item The length of the time series must be greater than n 10 | ##' } 11 | ##' Otherwise the growth will not be computed. 12 | ##' 13 | ##' @param x The time series for the change to be calculated. 14 | ##' @param n The period for the growth to be calculated over. 15 | ##' @return The n-period change of the time series. 16 | ##' @export 17 | ##' @examples 18 | ##' test.ts = abs(rnorm(100)) 19 | ##' chgr(test.ts, 1) 20 | ##' chgr(test.ts, 3) 21 | ##' chgr(test.ts, 10) 22 | 23 | chgr = function(x, n = 1){ 24 | T = length(x) 25 | if(sum(is.na(x)) == T){ 26 | chgr = rep(NA, T) 27 | warning("All values are NA") 28 | } else { 29 | firstObs = ifelse(any(is.na(x)), min(c(1, which(!is.na(x)))), 1) 30 | if(n > T - firstObs - 1){ 31 | chgr = rep(NA, T) 32 | warning("Time series does not have sufficient values") 33 | } else { 34 | if(sum(is.na(x[firstObs:T])) > 0.5 * (T - firstObs + 1)){ 35 | chgr = rep(NA, T) 36 | warning("Over 50% of the data are missing") 37 | } else { 38 | chgr = double(T) 39 | chgr[1:(firstObs + n - 1)] = NA 40 | chgr[(firstObs + n):T] = diff(x[(firstObs):T], n) 41 | } 42 | } 43 | } 44 | chgr 45 | } 46 | -------------------------------------------------------------------------------- /FAOSTAT/R/chgr.R: -------------------------------------------------------------------------------- 1 | ##' Absolute change between the year 2 | ##' 3 | ##' Function for generating the n-period absolute change 4 | ##' 5 | ##' In order to ensure the change calculated is reliable, the 6 | ##' following rule are applied. 7 | ##' \enumerate{ 8 | ##' \item 50\% of the data must be present. 9 | ##' \item The length of the time series must be greater than n 10 | ##' } 11 | ##' Otherwise the growth will not be computed. 12 | ##' 13 | ##' @param x The time series for the change to be calculated. 14 | ##' @param n The period for the growth to be calculated over. 15 | ##' @return The n-period change of the time series. 16 | ##' @export 17 | ##' @examples 18 | ##' test.ts = abs(rnorm(100)) 19 | ##' chgr(test.ts, 1) 20 | ##' chgr(test.ts, 3) 21 | ##' chgr(test.ts, 10) 22 | 23 | chgr = function(x, n = 1){ 24 | T = length(x) 25 | if(sum(is.na(x)) == T){ 26 | chgr = rep(NA, T) 27 | warning("All values are NA") 28 | } else { 29 | firstObs = ifelse(any(is.na(x)), min(c(1, which(!is.na(x)))), 1) 30 | if(n > T - firstObs - 1){ 31 | chgr = rep(NA, T) 32 | warning("Time series does not have sufficient values") 33 | } else { 34 | if(sum(is.na(x[firstObs:T])) > 0.5 * (T - firstObs + 1)){ 35 | chgr = rep(NA, T) 36 | warning("Over 50% of the data are missing") 37 | } else { 38 | chgr = double(T) 39 | chgr[1:(firstObs + n - 1)] = NA 40 | chgr[(firstObs + n):T] = diff(x[(firstObs):T], n) 41 | } 42 | } 43 | } 44 | chgr 45 | } 46 | -------------------------------------------------------------------------------- /Codes/R/fillCountryCode.R: -------------------------------------------------------------------------------- 1 | ##' A function to get country code when not available in data. 2 | ##' 3 | ##' This function can be useful when a dataset provided does not have 4 | ##' a country code available. 5 | ##' 6 | ##' @param country The column name of the data which contains 7 | ##' the country name 8 | ##' @param data The data frame to be matched 9 | ##' @param outCode The output country code system, defaulted to FAO standard. 10 | ##' @export 11 | 12 | fillCountryCode = function(country, data, outCode = "FAOST_CODE"){ 13 | unqCountry = unique(data[, country]) 14 | n = length(unqCountry) 15 | countryCODE = rep(NA, n) 16 | for(i in 1:n){ 17 | ind = which(as.matrix(FAOcountryProfile[, 18 | c("OFFICIAL_FAO_NAME", "SHORT_NAME", "FAO_TABLE_NAME", 19 | "UNOFFICIAL1_NAME", "UNOFFICIAL2_NAME", "UNOFFICIAL3_NAME")]) == 20 | unqCountry[i], arr.ind = TRUE) 21 | which.row = ind[, 1] 22 | if(length(unique(which.row)) == 1) 23 | countryCODE[i] = FAOcountryProfile[unique(which.row), outCode] 24 | } 25 | if(anyDuplicated(na.omit(countryCODE))) 26 | warning(paste0("Duplicated ", outCode, " matched, double check the data")) 27 | if(any(is.na(countryCODE))) 28 | warning(paste0("Certain ", outCode , " were not matched.")) 29 | warning("Please check the correct China has been specified.") 30 | def = data.frame(unqCountry, countryCODE) 31 | colnames(def) = c(country, outCode) 32 | merge(x = data, y = def, by = country, all.x = TRUE) 33 | } 34 | 35 | utils::globalVariables(names = c("FAOcountryProfile")) -------------------------------------------------------------------------------- /FAOSTAT/R/fillCountryCode.R: -------------------------------------------------------------------------------- 1 | ##' A function to get country code when not available in data. 2 | ##' 3 | ##' This function can be useful when a dataset provided does not have 4 | ##' a country code available. 5 | ##' 6 | ##' @param country The column name of the data which contains 7 | ##' the country name 8 | ##' @param data The data frame to be matched 9 | ##' @param outCode The output country code system, defaulted to FAO standard. 10 | ##' @export 11 | 12 | fillCountryCode = function(country, data, outCode = "FAOST_CODE"){ 13 | unqCountry = unique(data[, country]) 14 | n = length(unqCountry) 15 | countryCODE = rep(NA, n) 16 | for(i in 1:n){ 17 | ind = which(as.matrix(FAOcountryProfile[, 18 | c("OFFICIAL_FAO_NAME", "SHORT_NAME", "FAO_TABLE_NAME", 19 | "UNOFFICIAL1_NAME", "UNOFFICIAL2_NAME", "UNOFFICIAL3_NAME")]) == 20 | unqCountry[i], arr.ind = TRUE) 21 | which.row = ind[, 1] 22 | if(length(unique(which.row)) == 1) 23 | countryCODE[i] = FAOcountryProfile[unique(which.row), outCode] 24 | } 25 | if(anyDuplicated(na.omit(countryCODE))) 26 | warning(paste0("Duplicated ", outCode, " matched, double check the data")) 27 | if(any(is.na(countryCODE))) 28 | warning(paste0("Certain ", outCode , " were not matched.")) 29 | warning("Please check the correct China has been specified.") 30 | def = data.frame(unqCountry, countryCODE) 31 | colnames(def) = c(country, outCode) 32 | merge(x = data, y = def, by = country, all.x = TRUE) 33 | } 34 | 35 | utils::globalVariables(names = c("FAOcountryProfile")) -------------------------------------------------------------------------------- /FAOSTAT/man/shConstruct.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/shConstruct.R 3 | \name{shConstruct} 4 | \alias{shConstruct} 5 | \title{Construct share variable} 6 | \usage{ 7 | shConstruct(data, totVar, shareVar, newVarName = NA) 8 | } 9 | \arguments{ 10 | \item{data}{The data frame containing both the share variable and 11 | the aggregated variable} 12 | 13 | \item{totVar}{The aggregated variable.} 14 | 15 | \item{shareVar}{The subset of the aggregated variable which to be 16 | divided by.} 17 | 18 | \item{newVarName}{The name assigned to the new variable, if missing 19 | then .SC/.SH/.GR will be appended depending on the type of 20 | construction} 21 | } 22 | \value{ 23 | A data frame with the new constructed variable 24 | } 25 | \description{ 26 | A function for constructing the share of a variable of an aggregated 27 | variable. 28 | } 29 | \details{ 30 | The share of a variable can be share of the World (if additional 31 | variable were not supplied) or share of another variable (per Capita 32 | if population was supplied). 33 | } 34 | \examples{ 35 | ## Total variables provided, scale by totVar 36 | test.df = data.frame(FAOST_CODE = 1, Year = 1990:1994, a = 1:5, b = 1:5) 37 | shConstruct(data = test.df, totVar = "a", shareVar = "b") 38 | 39 | ## Total variables not provided, scale by world aggregate. 40 | test.df2 = data.frame(FAOST_CODE = rep(c(1, 5000), each = 5), 41 | Year = rep(1990:1994, 2), 42 | a = rep(1:5, 2), b = rep(1:5, 2)) 43 | shConstruct(data = test.df2, totVar = NA, shareVar = "b") 44 | } 45 | -------------------------------------------------------------------------------- /Codes/R/translateUnit.R: -------------------------------------------------------------------------------- 1 | ##' Function to translate multipliers 2 | ##' 3 | ##' This function translates number to character name or vice versa 4 | ##' 5 | ##' @param vec The vector containing name or number to be translated 6 | ##' @export 7 | ##' 8 | ##' @examples 9 | ##' ## Create numeric vector 10 | ##' myUnit = c(1000, 1e6, 1000, 1e9, 1e9, 1e12) 11 | ##' 12 | ##' ## Translate numeric to character 13 | ##' myUnit2 = translateUnit(myUnit) 14 | ##' myUnit2 15 | ##' 16 | ##' ## Now translate back 17 | ##' translateUnit(myUnit2) 18 | ##' 19 | 20 | translateUnit = function(vec){ 21 | type = mode(vec) 22 | if(type == "character"){ 23 | if(!(all(unique(vec) %in% c("hundred", "thousand", "million", 24 | "billion", "trillion", NA)))) 25 | stop("Unrecognised name") 26 | transVec = double(length(vec)) 27 | transVec[which(vec == "hundred")] = 100 28 | transVec[which(vec == "thousand")] = 1000 29 | transVec[which(vec == "million")] = 1e6 30 | transVec[which(vec == "billion")] = 1e9 31 | transVec[which(vec == "trillion")] = 1e12 32 | transVec[is.na(vec)] = NA 33 | } else if(type == "numeric"){ 34 | if(!(all(unique(vec) %in% c(100, 1000, 1e6, 1e9, 1e12, NA)))) 35 | stop("The unit does not have a character name available to translate") 36 | transVec = character(length(vec)) 37 | transVec[which(vec == 100)] = "hundred" 38 | transVec[which(vec == 1000)] = "thousand" 39 | transVec[which(vec == 1e6)] = "million" 40 | transVec[which(vec == 1e9)] = "billion" 41 | transVec[which(vec == 1e12)] = "trillion" 42 | transVec[is.na(vec)] = NA 43 | } else { 44 | stop("The type of vector can not be translated") 45 | } 46 | names(transVec) = names(vec) 47 | transVec 48 | } 49 | -------------------------------------------------------------------------------- /FAOSTAT/R/translateUnit.R: -------------------------------------------------------------------------------- 1 | ##' Function to translate multipliers 2 | ##' 3 | ##' This function translates number to character name or vice versa 4 | ##' 5 | ##' @param vec The vector containing name or number to be translated 6 | ##' @export 7 | ##' 8 | ##' @examples 9 | ##' ## Create numeric vector 10 | ##' myUnit = c(1000, 1e6, 1000, 1e9, 1e9, 1e12) 11 | ##' 12 | ##' ## Translate numeric to character 13 | ##' myUnit2 = translateUnit(myUnit) 14 | ##' myUnit2 15 | ##' 16 | ##' ## Now translate back 17 | ##' translateUnit(myUnit2) 18 | ##' 19 | 20 | translateUnit = function(vec){ 21 | type = mode(vec) 22 | if(type == "character"){ 23 | if(!(all(unique(vec) %in% c("hundred", "thousand", "million", 24 | "billion", "trillion", NA)))) 25 | stop("Unrecognised name") 26 | transVec = double(length(vec)) 27 | transVec[which(vec == "hundred")] = 100 28 | transVec[which(vec == "thousand")] = 1000 29 | transVec[which(vec == "million")] = 1e6 30 | transVec[which(vec == "billion")] = 1e9 31 | transVec[which(vec == "trillion")] = 1e12 32 | transVec[is.na(vec)] = NA 33 | } else if(type == "numeric"){ 34 | if(!(all(unique(vec) %in% c(100, 1000, 1e6, 1e9, 1e12, NA)))) 35 | stop("The unit does not have a character name available to translate") 36 | transVec = character(length(vec)) 37 | transVec[which(vec == 100)] = "hundred" 38 | transVec[which(vec == 1000)] = "thousand" 39 | transVec[which(vec == 1e6)] = "million" 40 | transVec[which(vec == 1e9)] = "billion" 41 | transVec[which(vec == 1e12)] = "trillion" 42 | transVec[is.na(vec)] = NA 43 | } else { 44 | stop("The type of vector can not be translated") 45 | } 46 | names(transVec) = names(vec) 47 | transVec 48 | } 49 | -------------------------------------------------------------------------------- /Codes/R/indConstruct.R: -------------------------------------------------------------------------------- 1 | ##' Construct indices 2 | ##' 3 | ##' A function for constructing indices 4 | ##' 5 | ##' @param data The data frame containing the data 6 | ##' @param origVar The variable in which the indices is to be computed 7 | ##' @param newVarName The name assigned to the new variable, if missing 8 | ##' then .SC/.SH/.GR/.CH/.IND will be appended depending on the type of 9 | ##' construction. 10 | ##' @param baseYear The year which will serve as the base 11 | ##' @return The indice 12 | ##' @export 13 | ##' @examples 14 | ##' test.df = data.frame(FAOST_CODE = rep(1, 100), Year = 1901:2000, 15 | ##' test = 1:100) 16 | ##' indConstruct(test.df, origVar = "test", baseYear = 1950) 17 | ##' 18 | indConstruct = function(data, origVar, newVarName = NA, baseYear = 2000){ 19 | tmp = arrange(subset(data, select = c("FAOST_CODE", "Year", origVar)), 20 | FAOST_CODE, Year) 21 | unqCountry = unique(tmp$FAOST_CODE) 22 | indVar = double() 23 | for(i in 1:length(unqCountry)){ 24 | allValues = unlist(subset(tmp, select = origVar, 25 | subset = FAOST_CODE == unqCountry[i])) 26 | baseYearValue = unlist(subset(tmp, select = origVar, 27 | subset = FAOST_CODE == unqCountry[i] & 28 | Year == baseYear)) 29 | ## Make the index NA if the base year value is not available 30 | if(length(baseYearValue) == 0) 31 | baseYearValue = NA 32 | tmpind = allValues/baseYearValue * 100 33 | indVar = c(indVar, tmpind) 34 | } 35 | ind.df = data.frame(tmp[c("FAOST_CODE", "Year")], indVar) 36 | colnames(ind.df)[3] = ifelse(is.na(newVarName), 37 | paste(origVar, ".IND", sep = ""), 38 | newVarName) 39 | ind.df 40 | } 41 | 42 | utils::globalVariables(names = "FAOST_CODE") 43 | -------------------------------------------------------------------------------- /FAOSTAT/R/indConstruct.R: -------------------------------------------------------------------------------- 1 | ##' Construct indices 2 | ##' 3 | ##' A function for constructing indices 4 | ##' 5 | ##' @param data The data frame containing the data 6 | ##' @param origVar The variable in which the indices is to be computed 7 | ##' @param newVarName The name assigned to the new variable, if missing 8 | ##' then .SC/.SH/.GR/.CH/.IND will be appended depending on the type of 9 | ##' construction. 10 | ##' @param baseYear The year which will serve as the base 11 | ##' @return The indice 12 | ##' @export 13 | ##' @examples 14 | ##' test.df = data.frame(FAOST_CODE = rep(1, 100), Year = 1901:2000, 15 | ##' test = 1:100) 16 | ##' indConstruct(test.df, origVar = "test", baseYear = 1950) 17 | ##' 18 | indConstruct = function(data, origVar, newVarName = NA, baseYear = 2000){ 19 | tmp = arrange(subset(data, select = c("FAOST_CODE", "Year", origVar)), 20 | FAOST_CODE, Year) 21 | unqCountry = unique(tmp$FAOST_CODE) 22 | indVar = double() 23 | for(i in 1:length(unqCountry)){ 24 | allValues = unlist(subset(tmp, select = origVar, 25 | subset = FAOST_CODE == unqCountry[i])) 26 | baseYearValue = unlist(subset(tmp, select = origVar, 27 | subset = FAOST_CODE == unqCountry[i] & 28 | Year == baseYear)) 29 | ## Make the index NA if the base year value is not available 30 | if(length(baseYearValue) == 0) 31 | baseYearValue = NA 32 | tmpind = allValues/baseYearValue * 100 33 | indVar = c(indVar, tmpind) 34 | } 35 | ind.df = data.frame(tmp[c("FAOST_CODE", "Year")], indVar) 36 | colnames(ind.df)[3] = ifelse(is.na(newVarName), 37 | paste(origVar, ".IND", sep = ""), 38 | newVarName) 39 | ind.df 40 | } 41 | 42 | utils::globalVariables(names = "FAOST_CODE") 43 | -------------------------------------------------------------------------------- /FAOSTAT/man/constructSYB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/constructSYB.R 3 | \name{constructSYB} 4 | \alias{constructSYB} 5 | \title{Construct/Creat new variable.} 6 | \usage{ 7 | constructSYB( 8 | data, 9 | origVar1, 10 | origVar2, 11 | newVarName = NA, 12 | constructType = c("share", "growth", "change", "index"), 13 | grFreq = 1, 14 | grType = c("ls", "geo"), 15 | baseYear = 2000 16 | ) 17 | } 18 | \arguments{ 19 | \item{data}{The data frame containing the raw variable} 20 | 21 | \item{origVar1}{The variable name to be used in construction, refer 22 | to Details for more information and useage.} 23 | 24 | \item{origVar2}{The variable name to be used in construction, refer 25 | to Details for more information and useage.} 26 | 27 | \item{newVarName}{The name assigned to the new variable, if missing 28 | then .SC/.SH/.GR/.CH will be appended depending on the type of 29 | construction} 30 | 31 | \item{constructType}{The type of construction, refer to Details 32 | for more information.} 33 | 34 | \item{grFreq}{The frequency for the growth rate to be computed.} 35 | 36 | \item{grType}{The method for the growth to be calculated, currently 37 | supports least squares and geometric.} 38 | 39 | \item{baseYear}{The base year to be used for constructing index.} 40 | } 41 | \value{ 42 | A data frame containing both the original data frame and the 43 | processed data and also a list indicating whether the construction 44 | passed or failed. 45 | } 46 | \description{ 47 | A function used to construct new variables from existing variables. 48 | } 49 | \details{ 50 | Currently two types of construction are supported, either share or 51 | growth rate computation. 52 | 53 | Share can be a share of total or share of another variable depending 54 | on whether an additional variable is supplied or not. 55 | } 56 | -------------------------------------------------------------------------------- /FAOSTAT/man/getWDItoSYB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getWDItoSYB.R 3 | \name{getWDItoSYB} 4 | \alias{getWDItoSYB} 5 | \title{Access to World Bank WDI API} 6 | \usage{ 7 | getWDItoSYB( 8 | indicator = "SP.POP.0014.TO.ZS", 9 | name = NULL, 10 | startDate = 1960, 11 | endDate = format(Sys.Date(), "\%Y"), 12 | printURL = FALSE, 13 | getMetaData = TRUE, 14 | printMetaData = FALSE, 15 | saveMetaData = FALSE, 16 | outputFormat = c("wide", "long") 17 | ) 18 | } 19 | \arguments{ 20 | \item{indicator}{The World Bank official indicator name.} 21 | 22 | \item{name}{The new name to be used in the column.} 23 | 24 | \item{startDate}{The start date for the data to begin} 25 | 26 | \item{endDate}{The end date.} 27 | 28 | \item{printURL}{Whether the url link for the data should be printed} 29 | 30 | \item{getMetaData}{Whether the data definition and the meta data 31 | should be downloaded as well.} 32 | 33 | \item{printMetaData}{logical, print out the meta data information} 34 | 35 | \item{saveMetaData}{logical, whether meta data should be saved as a 36 | local csv file} 37 | 38 | \item{outputFormat}{The format of the data, can be 'long' or 'wide'.} 39 | } 40 | \value{ 41 | A list containing the following elements 42 | \describe{ 43 | \item{data}{The country level data} 44 | \item{aggregates}{The aggregates provided by the World Bank} 45 | \item{metaData}{The metaData associated with the data} 46 | \item{results}{The status of the download, whether success/failed} 47 | } 48 | } 49 | \description{ 50 | The function downloads data from the World Bank API. 51 | } 52 | \examples{ 53 | ## pop.df = getWDItoSYB(name = "total_population", 54 | ## indicator = "SP.POP.TOTL") 55 | 56 | 57 | } 58 | \seealso{ 59 | \code{\link{getWDI}}, \code{\link{getFAO}}, 60 | \code{\link{getFAOtoSYB}} 61 | } 62 | -------------------------------------------------------------------------------- /FAOSTAT/man/getFAO.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getFAO.R 3 | \name{getFAO} 4 | \alias{getFAO} 5 | \title{Access to FAO FAOSTAT API.} 6 | \usage{ 7 | getFAO( 8 | name = NULL, 9 | domainCode = "RL", 10 | elementCode = 5110, 11 | itemCode = 6621, 12 | query, 13 | printURL = FALSE, 14 | useCHMT = TRUE, 15 | outputFormat = "wide", 16 | returnNames = FALSE, 17 | returnFlags = FALSE, 18 | yearRange = NULL, 19 | countrySet = NULL 20 | ) 21 | } 22 | \arguments{ 23 | \item{name}{The name to be given to the variable.} 24 | 25 | \item{domainCode}{The domain of the data.} 26 | 27 | \item{elementCode}{The code of the element.} 28 | 29 | \item{itemCode}{The code of the specific item.} 30 | 31 | \item{query}{The object created if using the FAOsearch function.} 32 | 33 | \item{printURL}{Whether the url link for the data should be printed.} 34 | 35 | \item{useCHMT}{logical, whether the CHMT function should be 36 | applied to avoid double counting of China.} 37 | 38 | \item{outputFormat}{The format of the data, can be 'long' or 'wide'.} 39 | 40 | \item{returnNames}{Logical, should the area, the element and the 41 | item names be reported?.} 42 | 43 | \item{returnFlags, }{Logical, whether the flags should be 44 | returned. Only work with outputFormat long.} 45 | 46 | \item{yearRange}{A numeric vector containing the years to be downloaded.} 47 | 48 | \item{countrySet}{The FAOSTAT codes of those countries to be downloaded.} 49 | } 50 | \value{ 51 | Outputs a data frame containing the specified data. 52 | } 53 | \description{ 54 | A function to access FAOSTAT data through the FAOSTAT API. 55 | } 56 | \details{ 57 | Need to account for multiple itemCode, currently only support one 58 | single variable. 59 | } 60 | \seealso{ 61 | \code{\link{getWDI}}, \code{\link{getWDItoSYB}}, 62 | \code{\link{getFAOtoSYB}}, \code{\link{FAOsearch}} 63 | } 64 | -------------------------------------------------------------------------------- /FAOSTAT/R/faostat_bulk_download.R: -------------------------------------------------------------------------------- 1 | #' Download bulk data from the faostat website 2 | #' http://www.fao.org/faostat/en/#data 3 | #' 4 | #' Note the files called "normalized" are in long format 5 | #' with a year column instead of one column for each year. 6 | #' The long format is preferable for data analysis. 7 | #' @param url_bulk character url of the faostat bulk zip file to download 8 | #' @param data_folder character path of the local folder where to download the data 9 | #' @author Paul Rougieux 10 | #' @examples 11 | #' \dontrun{ 12 | #' # Load global forestry data in long format 13 | #' url_forestry <- "http://fenixservices.fao.org/faostat/static/bulkdownloads/Forestry_E_All_Data_(Normalized).zip" 14 | #' download_faostat_bulk(url_forestry) 15 | #' } 16 | #' @export 17 | download_faostat_bulk <- function(url_bulk, data_folder="data_raw"){ 18 | file_name <- basename(url_bulk) 19 | download.file(url_bulk, file.path(data_folder, file_name)) 20 | } 21 | 22 | #' Function to read zipped files and return a data frame 23 | #' Reads the main csv file within the archive. 24 | #' The main file has the same name as the name of the archive. 25 | #' Note: the zip archive might also contain metadata files about Flags and Symboles. 26 | #' @param zip_file_name character name of the zip file to read 27 | #' @return data frame of FAOSTAT data 28 | #' @examples 29 | #' \dontrun{ 30 | #' # Read a file then assign it to a data frame and save it as rds 31 | #' forestry_e_all_data <- read_faostat_bulk("data_raw/Forestry_E_All_Data_(Normalized).zip") 32 | #' saveRDS(forestry_e_all_data,"data_raw/forestry_e_all_data.rds") 33 | #' } 34 | #' @export 35 | read_faostat_bulk <- function(zip_file_name){ 36 | # The main csv file shares the name of the archive 37 | csv_file_name <- gsub(".zip$",".csv", basename(zip_file_name)) 38 | # Read the csv file within the zip file 39 | df <- read.csv(unz(zip_file_name, csv_file_name), stringsAsFactors = FALSE) 40 | return(df) 41 | } 42 | -------------------------------------------------------------------------------- /Codes/R/mergeSYB.R: -------------------------------------------------------------------------------- 1 | ##' Function for merging data from different source. 2 | ##' 3 | ##' This function searches for supported country system and translate 4 | ##' the data to allow for join. 5 | ##' 6 | ##' The names of the data to be merged has to be the same as the 7 | ##' FAOcountryProfile code name. 8 | ##' 9 | ##' @param x data frames, or objects to be coerced to one. 10 | ##' @param y data frames, or objects to be coerced to one. 11 | ##' @param outCode The country code system to be used to join the 12 | ##' different sources. 13 | ##' @param all Same as the merge function, defaulted to an outer join. 14 | ##' @param ... Arguments to be passed on to the merge function. 15 | ##' 16 | ##' @export 17 | 18 | mergeSYB = function(x, y, outCode = "FAOST_CODE", all = TRUE, ...){ 19 | ## Translate code in x to outCode 20 | fromCodex = intersect(colnames(x), colnames(FAOcountryProfile)) 21 | if(length(fromCodex) == 0){ 22 | stop("Unknown country code in x, check FAOcountryProfile for supported country code") 23 | } else if(length(fromCodex) > 1){ 24 | warning(paste("More than one country code system found in x, ", 25 | fromCodex[1], " is used.", sep = "")) 26 | fromCodex = fromCodex[1] 27 | } 28 | dfx = translateCountryCode(data = x, from = fromCodex, to = outCode) 29 | ## Translate code in y to outCode 30 | fromCodey = intersect(colnames(y), colnames(FAOcountryProfile)) 31 | if(length(fromCodey) == 0){ 32 | stop("Unknown country code in y, check FAOcountryProfile for supported country code") 33 | } else if(length(fromCodey) > 1){ 34 | warning(paste("More than one country code system found in y, ", 35 | fromCodey[1], " is used.", sep = "")) 36 | fromCodey = fromCodey[1] 37 | } 38 | dfy = translateCountryCode(data = y, from = fromCodey, to = outCode) 39 | ## Merge the translated data 40 | merge(x = dfx, y = dfy, all = TRUE, ...) 41 | } 42 | 43 | utils::globalVariables(names = c("FAOcountryProfile")) -------------------------------------------------------------------------------- /FAOSTAT/R/mergeSYB.R: -------------------------------------------------------------------------------- 1 | ##' Function for merging data from different source. 2 | ##' 3 | ##' This function searches for supported country system and translate 4 | ##' the data to allow for join. 5 | ##' 6 | ##' The names of the data to be merged has to be the same as the 7 | ##' FAOcountryProfile code name. 8 | ##' 9 | ##' @param x data frames, or objects to be coerced to one. 10 | ##' @param y data frames, or objects to be coerced to one. 11 | ##' @param outCode The country code system to be used to join the 12 | ##' different sources. 13 | ##' @param all Same as the merge function, defaulted to an outer join. 14 | ##' @param ... Arguments to be passed on to the merge function. 15 | ##' 16 | ##' @export 17 | 18 | mergeSYB = function(x, y, outCode = "FAOST_CODE", all = TRUE, ...){ 19 | ## Translate code in x to outCode 20 | fromCodex = intersect(colnames(x), colnames(FAOcountryProfile)) 21 | if(length(fromCodex) == 0){ 22 | stop("Unknown country code in x, check FAOcountryProfile for supported country code") 23 | } else if(length(fromCodex) > 1){ 24 | warning(paste("More than one country code system found in x, ", 25 | fromCodex[1], " is used.", sep = "")) 26 | fromCodex = fromCodex[1] 27 | } 28 | dfx = translateCountryCode(data = x, from = fromCodex, to = outCode) 29 | ## Translate code in y to outCode 30 | fromCodey = intersect(colnames(y), colnames(FAOcountryProfile)) 31 | if(length(fromCodey) == 0){ 32 | stop("Unknown country code in y, check FAOcountryProfile for supported country code") 33 | } else if(length(fromCodey) > 1){ 34 | warning(paste("More than one country code system found in y, ", 35 | fromCodey[1], " is used.", sep = "")) 36 | fromCodey = fromCodey[1] 37 | } 38 | dfy = translateCountryCode(data = y, from = fromCodey, to = outCode) 39 | ## Merge the translated data 40 | merge(x = dfx, y = dfy, all = TRUE, ...) 41 | } 42 | 43 | utils::globalVariables(names = c("FAOcountryProfile")) -------------------------------------------------------------------------------- /FAOSTAT/man/getFAOtoSYB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getFAOtoSYB.R 3 | \name{getFAOtoSYB} 4 | \alias{getFAOtoSYB} 5 | \title{Access to FAO FAOSTAT API} 6 | \usage{ 7 | getFAOtoSYB( 8 | name = NULL, 9 | domainCode = "RL", 10 | elementCode = 5110, 11 | itemCode = 6621, 12 | query, 13 | printURL = FALSE, 14 | useCHMT = TRUE, 15 | yearRange = NULL, 16 | countrySet = NULL, 17 | outputFormat = c("wide", "long"), 18 | returnFlags = FALSE 19 | ) 20 | } 21 | \arguments{ 22 | \item{name}{The name to be given to the variable.} 23 | 24 | \item{domainCode}{The domain code of the variable, see details.} 25 | 26 | \item{elementCode}{The element code of the variable, see details.} 27 | 28 | \item{itemCode}{The item code of the variable, see details.} 29 | 30 | \item{query}{The object created if using the FAOsearch function} 31 | 32 | \item{printURL}{Whether the url link for the data should be printed} 33 | 34 | \item{useCHMT}{logical, whether the CHMT function should be} 35 | 36 | \item{yearRange}{A numeric vector containing the years to be downloaded.} 37 | 38 | \item{countrySet}{The FAOSTAT codes of those countries to be downloaded.} 39 | 40 | \item{outputFormat}{The format of the data, can be 'long' or 'wide'. 41 | appied to avoid double counting of China.} 42 | 43 | \item{returnFlags, }{Logical, whether the flags should be 44 | returned. Only work with outputFormat long.} 45 | } 46 | \value{ 47 | A list containing the following elements 48 | \describe{ 49 | \item{entity}{The entity level data} 50 | \item{aggregates}{The aggregates provided by the FAO} 51 | \item{results}{The status of the download, whether success/failed} 52 | } 53 | } 54 | \description{ 55 | A wrapper function using getFAO() to obtain and process multiple 56 | data set to obtain data. 57 | } 58 | \examples{ 59 | ## The default option is the arable land area 60 | ## arlLand.lst = getFAOtoSYB() 61 | } 62 | \seealso{ 63 | \code{\link{getWDI}}, \code{\link{getFAO}}, 64 | \code{\link{getWDItoSYB}} 65 | } 66 | -------------------------------------------------------------------------------- /Codes/R/scaleUnit.R: -------------------------------------------------------------------------------- 1 | ##' A function to standardize the unit 2 | ##' 3 | ##' The function standardize the data to the desirable unit when the 4 | ##' multiplier vector is supplied. For example per 1000 people is 5 | ##' scaled to per person by supplying a multiplier of 1000. 6 | ##' 7 | ##' @param df The data frame containing the data to be scale 8 | ##' @param multiplier The named vector with the multiplier to be 9 | ##' scaled. The name is mandatory in order for the function to identify 10 | ##' the variable in the data frame. A data.frame can also be supplied 11 | ##' with the first column being the name and the second being the 12 | ##' numeric multiplier. 13 | ##' @export 14 | ##' @examples 15 | ##' 16 | ##' ## Create the data frame 17 | ##' test.df = data.frame(FAOST_CODE = 1:5, Year = 1995:1999, 18 | ##' var1 = 1:5, var2 = 5:1) 19 | ##' 20 | ##' ## Create the named vector for scaling 21 | ##' multiplier = c(1, 10) 22 | ##' names(multiplier) = c("var1", "var2") 23 | ##' 24 | ##' ## Scale the data 25 | ##' scaleUnit(test.df, multiplier = multiplier) 26 | 27 | scaleUnit = function(df, multiplier){ 28 | printLab("Scaling to base unit") 29 | cat("\t\tNOTE: This function should only be performed once\n\n") 30 | if(is.data.frame(multiplier) & NCOL(multiplier) == 2){ 31 | colModes = sapply(multiplier, mode) 32 | if(any(colModes == "numeric") && any(colModes == "character")){ 33 | multiplierVec = multiplier[, which(colModes == "numeric")] 34 | names(multiplierVec) = multiplier[, which(colModes == "character")] 35 | } else { 36 | stop("Invalid multiplier data.frame") 37 | } 38 | } else if(is.vector(multiplier)){ 39 | multiplierVec = multiplier 40 | } else { 41 | stop("Invalid multiplier input") 42 | } 43 | if(is.null(names(multiplierVec))) 44 | stop("Please supply names to the multiplierVec vector") 45 | multiplierVec[is.na(multiplierVec)] = 1 46 | inIndex = which(names(multiplierVec) %in% colnames(df)) 47 | if(length(inIndex) != length(multiplierVec)){ 48 | cat("The following variables are not in the data frame:\n") 49 | cat(paste("\t", names(multiplierVec[-inIndex]), "\n", sep = "")) 50 | } 51 | n = NROW(df) 52 | multiplierVec.mat = matrix(rep(multiplierVec[inIndex], n), nrow = n, 53 | byrow = TRUE) 54 | df[, names(multiplierVec[inIndex])] = df[, names(multiplierVec[inIndex])] * 55 | multiplierVec.mat 56 | df 57 | } 58 | -------------------------------------------------------------------------------- /FAOSTAT/R/scaleUnit.R: -------------------------------------------------------------------------------- 1 | ##' A function to standardize the unit 2 | ##' 3 | ##' The function standardize the data to the desirable unit when the 4 | ##' multiplier vector is supplied. For example per 1000 people is 5 | ##' scaled to per person by supplying a multiplier of 1000. 6 | ##' 7 | ##' @param df The data frame containing the data to be scale 8 | ##' @param multiplier The named vector with the multiplier to be 9 | ##' scaled. The name is mandatory in order for the function to identify 10 | ##' the variable in the data frame. A data.frame can also be supplied 11 | ##' with the first column being the name and the second being the 12 | ##' numeric multiplier. 13 | ##' @export 14 | ##' @examples 15 | ##' 16 | ##' ## Create the data frame 17 | ##' test.df = data.frame(FAOST_CODE = 1:5, Year = 1995:1999, 18 | ##' var1 = 1:5, var2 = 5:1) 19 | ##' 20 | ##' ## Create the named vector for scaling 21 | ##' multiplier = c(1, 10) 22 | ##' names(multiplier) = c("var1", "var2") 23 | ##' 24 | ##' ## Scale the data 25 | ##' scaleUnit(test.df, multiplier = multiplier) 26 | 27 | scaleUnit = function(df, multiplier){ 28 | printLab("Scaling to base unit") 29 | cat("\t\tNOTE: This function should only be performed once\n\n") 30 | if(is.data.frame(multiplier) & NCOL(multiplier) == 2){ 31 | colModes = sapply(multiplier, mode) 32 | if(any(colModes == "numeric") && any(colModes == "character")){ 33 | multiplierVec = multiplier[, which(colModes == "numeric")] 34 | names(multiplierVec) = multiplier[, which(colModes == "character")] 35 | } else { 36 | stop("Invalid multiplier data.frame") 37 | } 38 | } else if(is.vector(multiplier)){ 39 | multiplierVec = multiplier 40 | } else { 41 | stop("Invalid multiplier input") 42 | } 43 | if(is.null(names(multiplierVec))) 44 | stop("Please supply names to the multiplierVec vector") 45 | multiplierVec[is.na(multiplierVec)] = 1 46 | inIndex = which(names(multiplierVec) %in% colnames(df)) 47 | if(length(inIndex) != length(multiplierVec)){ 48 | cat("The following variables are not in the data frame:\n") 49 | cat(paste("\t", names(multiplierVec[-inIndex]), "\n", sep = "")) 50 | } 51 | n = NROW(df) 52 | multiplierVec.mat = matrix(rep(multiplierVec[inIndex], n), nrow = n, 53 | byrow = TRUE) 54 | df[, names(multiplierVec[inIndex])] = df[, names(multiplierVec[inIndex])] * 55 | multiplierVec.mat 56 | df 57 | } 58 | -------------------------------------------------------------------------------- /Codes/R/overlap.R: -------------------------------------------------------------------------------- 1 | ##' This function checks whether there are overlapping between the 2 | ##' transitional countries. 3 | ##' 4 | ##' 5 | ##' @param old The FAOST_CODE of the old countries 6 | ##' @param new The FAOST_CODE of the new countries 7 | ##' @param var The variable to be checked 8 | ##' @param year The column which index the time. 9 | ##' @param data The data frame 10 | ##' @param take The type of check/replacement to be done. 11 | ##' 12 | ##' @export 13 | ##' 14 | 15 | 16 | overlap = function(old, new, var, year = "Year", data, take){ 17 | 18 | n.var = length(var) 19 | for(i in 1:n.var){ 20 | old_years = unique(data[data$FAOST_CODE %in% old & 21 | !is.na(data[, var[i]]), year]) 22 | new_years = unique(data[data$FAOST_CODE %in% new & 23 | !is.na(data[, var[i]]), year]) 24 | int_years = intersect(old_years, new_years) 25 | if(length(int_years) > 0) 26 | cat(paste0(var[i], " has overlap in:\n\t Year = ", 27 | paste0(int_years, collapse = ", "), 28 | "\n\tfor FAOST_CODE = ", 29 | paste0(c(old, new), collapse = ", "), "\n")) 30 | switch(take, 31 | simpleCheck = {}, 32 | takeNew = { 33 | data[data$FAOST_CODE %in% old & 34 | data[, year] %in% int_years, var[i]] = NA 35 | }, 36 | takeOld = { 37 | data[data$FAOST_CODE %in% new & 38 | data[, year] %in% int_years, var[i]] = NA 39 | }, 40 | complete = { 41 | for(j in sort(unique(int_years))){ 42 | if(NROW(data[data[, "FAOST_CODE"] %in% new & 43 | !is.na(data[, var[i]]) & data[, year] == j, "FAOST_CODE"]) == length(new)) { 44 | ## All the new entities are there. Remove the old. 45 | data[data[, "FAOST_CODE"] %in% old & data[, year] == j, var[i]] = NA 46 | } else { 47 | ## Not all the new entities are there. Remove the new. 48 | data[data[, "FAOST_CODE"] %in% new & data[, year] == j, var[i]] = NA 49 | } 50 | } 51 | } 52 | ) 53 | } 54 | data 55 | } 56 | -------------------------------------------------------------------------------- /FAOSTAT/R/overlap.R: -------------------------------------------------------------------------------- 1 | ##' This function checks whether there are overlapping between the 2 | ##' transitional countries. 3 | ##' 4 | ##' 5 | ##' @param old The FAOST_CODE of the old countries 6 | ##' @param new The FAOST_CODE of the new countries 7 | ##' @param var The variable to be checked 8 | ##' @param year The column which index the time. 9 | ##' @param data The data frame 10 | ##' @param take The type of check/replacement to be done. 11 | ##' 12 | ##' @export 13 | ##' 14 | 15 | 16 | overlap = function(old, new, var, year = "Year", data, take){ 17 | 18 | n.var = length(var) 19 | for(i in 1:n.var){ 20 | old_years = unique(data[data$FAOST_CODE %in% old & 21 | !is.na(data[, var[i]]), year]) 22 | new_years = unique(data[data$FAOST_CODE %in% new & 23 | !is.na(data[, var[i]]), year]) 24 | int_years = intersect(old_years, new_years) 25 | if(length(int_years) > 0) 26 | cat(paste0(var[i], " has overlap in:\n\t Year = ", 27 | paste0(int_years, collapse = ", "), 28 | "\n\tfor FAOST_CODE = ", 29 | paste0(c(old, new), collapse = ", "), "\n")) 30 | switch(take, 31 | simpleCheck = {}, 32 | takeNew = { 33 | data[data$FAOST_CODE %in% old & 34 | data[, year] %in% int_years, var[i]] = NA 35 | }, 36 | takeOld = { 37 | data[data$FAOST_CODE %in% new & 38 | data[, year] %in% int_years, var[i]] = NA 39 | }, 40 | complete = { 41 | for(j in sort(unique(int_years))){ 42 | if(NROW(data[data[, "FAOST_CODE"] %in% new & 43 | !is.na(data[, var[i]]) & data[, year] == j, "FAOST_CODE"]) == length(new)) { 44 | ## All the new entities are there. Remove the old. 45 | data[data[, "FAOST_CODE"] %in% old & data[, year] == j, var[i]] = NA 46 | } else { 47 | ## Not all the new entities are there. Remove the new. 48 | data[data[, "FAOST_CODE"] %in% new & data[, year] == j, var[i]] = NA 49 | } 50 | } 51 | } 52 | ) 53 | } 54 | data 55 | } 56 | -------------------------------------------------------------------------------- /Codes/R/getWDI.R: -------------------------------------------------------------------------------- 1 | ##' Access to World Bank WDI API 2 | ##' 3 | ##' A function to extract data from the World Bank API 4 | ##' 5 | ##' Please refer to \url{http://data.worldbank.org/node/18} for any 6 | ##' difference between the country code system. Further details on World 7 | ##' Bank classification and methodology are available at 8 | ##' \url{http://data.worldbank.org/}. 9 | ##' 10 | ##' @param indicator The World Bank official indicator name. 11 | ##' @param name The new name to be used in the column. 12 | ##' @param startDate The start date for the data to begin 13 | ##' @param endDate The end date. 14 | ##' @param printURL Whether the url link for the data should be printed 15 | ##' @param outputFormat The format of the data, can be 'long' or 'wide'. 16 | ##' @return A data frame containing the desired World Bank Indicator 17 | ##' @export 18 | ##' 19 | ##' @seealso \code{\link{getFAO}}, \code{\link{getWDItoSYB}}, 20 | ##' \code{\link{getFAOtoSYB}} 21 | ##' @examples 22 | ##' ## pop.df = getWDI() 23 | ##' 24 | 25 | ## source: 26 | ## http://lamages.blogspot.it/2011/09/setting-initial-view-of-motion-chart-in.html 27 | 28 | ## TODO (Michael): Investigate why sometimes ISO2 is used and 29 | ## sometiems ISO3 is used. 30 | getWDI = function(indicator = "SP.POP.TOTL", name = NULL, 31 | startDate = 1960, endDate = format(Sys.Date(), "%Y"), 32 | printURL = FALSE, outputFormat = "wide"){ 33 | if(is.null(name)) 34 | name = indicator 35 | url = paste("http://api.worldbank.org/countries/all/indicators/", indicator, 36 | "?date=", startDate, ":", endDate, 37 | "&format=json&per_page=30000", sep = "") 38 | if(printURL) 39 | print(url) 40 | wbData = fromJSON(url)[[2]] 41 | wbData = data.frame(Country = sapply(wbData, 42 | function(x) x[["country"]]["value"]), 43 | ISO2_WB_CODE= sapply(wbData, 44 | function(x) x[["country"]]["id"]), 45 | Year = as.integer(sapply(wbData, "[[", "date")), 46 | Value = as.numeric(sapply(wbData, function(x) 47 | ifelse(is.null(x[["value"]]), NA, x[["value"]]))), 48 | stringsAsFactors = FALSE) 49 | if(outputFormat == "long"){ 50 | wbData$name = name 51 | } else if(outputFormat == "wide"){ 52 | names(wbData)[4] = name 53 | } 54 | wbData 55 | } 56 | -------------------------------------------------------------------------------- /FAOSTAT/R/getWDI.R: -------------------------------------------------------------------------------- 1 | ##' Access to World Bank WDI API 2 | ##' 3 | ##' A function to extract data from the World Bank API 4 | ##' 5 | ##' Please refer to \url{http://data.worldbank.org/node/18} for any 6 | ##' difference between the country code system. Further details on World 7 | ##' Bank classification and methodology are available at 8 | ##' \url{http://data.worldbank.org/}. 9 | ##' 10 | ##' @param indicator The World Bank official indicator name. 11 | ##' @param name The new name to be used in the column. 12 | ##' @param startDate The start date for the data to begin 13 | ##' @param endDate The end date. 14 | ##' @param printURL Whether the url link for the data should be printed 15 | ##' @param outputFormat The format of the data, can be 'long' or 'wide'. 16 | ##' @return A data frame containing the desired World Bank Indicator 17 | ##' @export 18 | ##' 19 | ##' @seealso \code{\link{getFAO}}, \code{\link{getWDItoSYB}}, 20 | ##' \code{\link{getFAOtoSYB}} 21 | ##' @examples 22 | ##' ## pop.df = getWDI() 23 | ##' 24 | 25 | ## source: 26 | ## http://lamages.blogspot.it/2011/09/setting-initial-view-of-motion-chart-in.html 27 | 28 | ## TODO (Michael): Investigate why sometimes ISO2 is used and 29 | ## sometiems ISO3 is used. 30 | getWDI = function(indicator = "SP.POP.TOTL", name = NULL, 31 | startDate = 1960, endDate = format(Sys.Date(), "%Y"), 32 | printURL = FALSE, outputFormat = "wide"){ 33 | if(is.null(name)) 34 | name = indicator 35 | url = paste("http://api.worldbank.org/countries/all/indicators/", indicator, 36 | "?date=", startDate, ":", endDate, 37 | "&format=json&per_page=30000", sep = "") 38 | if(printURL) 39 | print(url) 40 | wbData = fromJSON(url)[[2]] 41 | wbData = data.frame(Country = sapply(wbData, 42 | function(x) x[["country"]]["value"]), 43 | ISO2_WB_CODE= sapply(wbData, 44 | function(x) x[["country"]]["id"]), 45 | Year = as.integer(sapply(wbData, "[[", "date")), 46 | Value = as.numeric(sapply(wbData, function(x) 47 | ifelse(is.null(x[["value"]]), NA, x[["value"]]))), 48 | stringsAsFactors = FALSE) 49 | if(outputFormat == "long"){ 50 | wbData$name = name 51 | } else if(outputFormat == "wide"){ 52 | names(wbData)[4] = name 53 | } 54 | wbData 55 | } 56 | -------------------------------------------------------------------------------- /Codes/R/translateCountryCode.R: -------------------------------------------------------------------------------- 1 | ##' A function to translate between different country coding systems 2 | ##' 3 | ##' The function translate any country code scheme to another if both 4 | ##' are in the FAOcountryProfile 5 | ##' 6 | ##' @param data The data frame 7 | ##' @param from The name of the old coding system 8 | ##' @param to The name of the new coding system 9 | ##' @param oldCode The column name of the old country coding scheme 10 | ##' @export 11 | ##' 12 | 13 | 14 | translateCountryCode = function (data, from, to, oldCode) 15 | { 16 | cat("\nNOTE: Please make sure that the country are matched according to their definition\n\n") 17 | if (missing(oldCode)) 18 | oldCode = from 19 | if (from != to) { 20 | codeTrans = FAOcountryProfile[which(FAOcountryProfile[, 21 | from] %in% data[, oldCode]), c(from, to)] 22 | trans.df = merge(x = codeTrans, y = data, by.x = from, 23 | by.y = oldCode, all.y = TRUE) 24 | if (any(is.na(trans.df[, to]))) { 25 | warning(paste("The following entries does not have '", 26 | to, "' available\n", sep = ""), immediate. = TRUE) 27 | print(FAOcountryProfile[FAOcountryProfile[, from] %in% 28 | trans.df[is.na(trans.df[, to]), from], 29 | c(from, to, "OFFICIAL_FAO_NAME")]) 30 | } 31 | trans.df$OFFICIAL_FAO_NAME = NULL 32 | } 33 | else { 34 | trans.df = data 35 | } 36 | trans.df 37 | } 38 | 39 | utils::globalVariables(names = c("FAOcountryProfile")) 40 | 41 | ## translateCountryCode = function(data, from, to, oldCode){ 42 | ## warning("Please make sure that the country are matched according to their definition") 43 | ## if(missing(oldCode)) 44 | ## oldCode = from 45 | ## if(from != to){ 46 | ## codeTrans = FAOcountryProfile[which(FAOcountryProfile[, from] %in% 47 | ## data[, oldCode]), c(from, to, "OFFICIAL_FAO_NAME")] 48 | ## trans.df = merge(x = codeTrans, y = data, by.x = from, 49 | ## by.y = oldCode, all.y = TRUE) 50 | ## if(any(is.na(trans.df[, to]))){ 51 | ## warning(paste("The following entries does not have '", 52 | ## to, "' available", sep = "")) 53 | ## print(unique(trans.df[is.na(trans.df[, to]), 54 | ## c(from, to, "OFFICIAL_FAO_NAME")])) 55 | ## } 56 | ## trans.df$OFFICIAL_FAO_NAME = NULL 57 | ## } else { 58 | ## trans.df = data 59 | ## } 60 | ## trans.df 61 | ## } 62 | -------------------------------------------------------------------------------- /FAOSTAT/R/translateCountryCode.R: -------------------------------------------------------------------------------- 1 | ##' A function to translate between different country coding systems 2 | ##' 3 | ##' The function translate any country code scheme to another if both 4 | ##' are in the FAOcountryProfile 5 | ##' 6 | ##' @param data The data frame 7 | ##' @param from The name of the old coding system 8 | ##' @param to The name of the new coding system 9 | ##' @param oldCode The column name of the old country coding scheme 10 | ##' @export 11 | ##' 12 | 13 | 14 | translateCountryCode = function (data, from, to, oldCode) 15 | { 16 | cat("\nNOTE: Please make sure that the country are matched according to their definition\n\n") 17 | if (missing(oldCode)) 18 | oldCode = from 19 | if (from != to) { 20 | codeTrans = FAOcountryProfile[which(FAOcountryProfile[, 21 | from] %in% data[, oldCode]), c(from, to)] 22 | trans.df = merge(x = codeTrans, y = data, by.x = from, 23 | by.y = oldCode, all.y = TRUE) 24 | if (any(is.na(trans.df[, to]))) { 25 | warning(paste("The following entries does not have '", 26 | to, "' available\n", sep = ""), immediate. = TRUE) 27 | print(FAOcountryProfile[FAOcountryProfile[, from] %in% 28 | trans.df[is.na(trans.df[, to]), from], 29 | c(from, to, "OFFICIAL_FAO_NAME")]) 30 | } 31 | trans.df$OFFICIAL_FAO_NAME = NULL 32 | } 33 | else { 34 | trans.df = data 35 | } 36 | trans.df 37 | } 38 | 39 | utils::globalVariables(names = c("FAOcountryProfile")) 40 | 41 | ## translateCountryCode = function(data, from, to, oldCode){ 42 | ## warning("Please make sure that the country are matched according to their definition") 43 | ## if(missing(oldCode)) 44 | ## oldCode = from 45 | ## if(from != to){ 46 | ## codeTrans = FAOcountryProfile[which(FAOcountryProfile[, from] %in% 47 | ## data[, oldCode]), c(from, to, "OFFICIAL_FAO_NAME")] 48 | ## trans.df = merge(x = codeTrans, y = data, by.x = from, 49 | ## by.y = oldCode, all.y = TRUE) 50 | ## if(any(is.na(trans.df[, to]))){ 51 | ## warning(paste("The following entries does not have '", 52 | ## to, "' available", sep = "")) 53 | ## print(unique(trans.df[is.na(trans.df[, to]), 54 | ## c(from, to, "OFFICIAL_FAO_NAME")])) 55 | ## } 56 | ## trans.df$OFFICIAL_FAO_NAME = NULL 57 | ## } else { 58 | ## trans.df = data 59 | ## } 60 | ## trans.df 61 | ## } 62 | -------------------------------------------------------------------------------- /Codes/R/CHMT.R: -------------------------------------------------------------------------------- 1 | ##' This function avoids double counting of China. 2 | ##' 3 | ##' This function should only be used when performing aggregations. 4 | ##' 5 | ##' We decide to use the smaller subsets in the regional level because 6 | ##' weighting variable may not exist for other variables for the 7 | ##' larger subsets. 8 | ##' 9 | ##' The function only work for FAOST_CODE, if the country coding 10 | ##' system is not in FAOST_CODE then use the translateCountryCode 11 | ##' function to translate it. 12 | ##' 13 | ##' @param var The variables that require to be sanitized. 14 | ##' @param data The data frame which contains the data 15 | ##' @param year The column which correspond to the year. 16 | ##' @export 17 | 18 | 19 | CHMT = function(var, data, year = "Year"){ 20 | for(i in 1:length(var)){ 21 | if(length(unique(data[data$FAOST_CODE %in% c(41, 351, 357) & 22 | !is.na(data[, var[i]]), "FAOST_CODE"])) > 1) { 23 | cat(paste0("\nNOTE: Multiple China detected in '", var[i], 24 | "' sanitization is performed\n")) 25 | for(j in sort(unique(data[, year]))){ 26 | ## If the China, mainland exist then we will not use 27 | ## (China + Taiwan) nor the (China + Hong Kong + Macau 28 | ## + Taiwan). 29 | if(NROW(data[data$FAOST_CODE == 41 & 30 | data[, year] == j, var[i]]) == 1){ 31 | if(!is.na(data[data$FAOST_CODE == 41 & 32 | data[, year] == j, var[i]])){ 33 | data[data$FAOST_CODE %in% c(351, 357), var[i]] = NA 34 | ## If China mainland does not exist then we 35 | ## will use (China + Taiwan). 36 | } else if(NROW(data[data$FAOST_CODE == 357 & 37 | data[, year] == j, var[i]]) == 1){ 38 | if(!is.na(data[data$FAOST_CODE == 357 & 39 | data[, year] == j, var[i]])){ 40 | data[data$FAOST_CODE %in% c(41, 214, 351), 41 | var[i]] = NA 42 | ## If both fails then we will use (China + 43 | ## Hong Kong + Macau + Taiwan) 44 | } else if(NROW(data[data$FAOST_CODE == 351 & 45 | data[, year] == j, var[i]]) == 1){ 46 | if(!is.na(data[data$FAOST_CODE == 351 & 47 | data[, year] == j, var[i]])){ 48 | data[data$FAOST_CODE %in% 49 | c(41, 96, 128, 214, 357), var[i]] = NA 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | data 58 | } 59 | -------------------------------------------------------------------------------- /FAOSTAT/R/CHMT.R: -------------------------------------------------------------------------------- 1 | ##' This function avoids double counting of China. 2 | ##' 3 | ##' This function should only be used when performing aggregations. 4 | ##' 5 | ##' We decide to use the smaller subsets in the regional level because 6 | ##' weighting variable may not exist for other variables for the 7 | ##' larger subsets. 8 | ##' 9 | ##' The function only work for FAOST_CODE, if the country coding 10 | ##' system is not in FAOST_CODE then use the translateCountryCode 11 | ##' function to translate it. 12 | ##' 13 | ##' @param var The variables that require to be sanitized. 14 | ##' @param data The data frame which contains the data 15 | ##' @param year The column which correspond to the year. 16 | ##' @export 17 | 18 | 19 | CHMT = function(var, data, year = "Year"){ 20 | for(i in 1:length(var)){ 21 | if(length(unique(data[data$FAOST_CODE %in% c(41, 351, 357) & 22 | !is.na(data[, var[i]]), "FAOST_CODE"])) > 1) { 23 | cat(paste0("\nNOTE: Multiple China detected in '", var[i], 24 | "' sanitization is performed\n")) 25 | for(j in sort(unique(data[, year]))){ 26 | ## If the China, mainland exist then we will not use 27 | ## (China + Taiwan) nor the (China + Hong Kong + Macau 28 | ## + Taiwan). 29 | if(NROW(data[data$FAOST_CODE == 41 & 30 | data[, year] == j, var[i]]) == 1){ 31 | if(!is.na(data[data$FAOST_CODE == 41 & 32 | data[, year] == j, var[i]])){ 33 | data[data$FAOST_CODE %in% c(351, 357), var[i]] = NA 34 | ## If China mainland does not exist then we 35 | ## will use (China + Taiwan). 36 | } else if(NROW(data[data$FAOST_CODE == 357 & 37 | data[, year] == j, var[i]]) == 1){ 38 | if(!is.na(data[data$FAOST_CODE == 357 & 39 | data[, year] == j, var[i]])){ 40 | data[data$FAOST_CODE %in% c(41, 214, 351), 41 | var[i]] = NA 42 | ## If both fails then we will use (China + 43 | ## Hong Kong + Macau + Taiwan) 44 | } else if(NROW(data[data$FAOST_CODE == 351 & 45 | data[, year] == j, var[i]]) == 1){ 46 | if(!is.na(data[data$FAOST_CODE == 351 & 47 | data[, year] == j, var[i]])){ 48 | data[data$FAOST_CODE %in% 49 | c(41, 96, 128, 214, 357), var[i]] = NA 50 | } 51 | } 52 | } 53 | } 54 | } 55 | } 56 | } 57 | data 58 | } 59 | -------------------------------------------------------------------------------- /Codes/R/grConstruct.R: -------------------------------------------------------------------------------- 1 | ##' Construct Growth rate 2 | ##' 3 | ##' A function for constructing growth rate variables. 4 | ##' 5 | ##' @param data The data frame containing the data 6 | ##' @param origVar The variable in which the growth is to be calculated 7 | ##' @param newVarName The name assigned to the new variable, if missing 8 | ##' then .SC/.SH/.GR will be appended depending on the type of 9 | ##' construction. 10 | ##' @param type The type of growth rate, can be least squares or 11 | ##' geometric 12 | ##' @param n The period for the growth rate to be calculated (Refer to 13 | ##' the lsgr or the geogr functions.) 14 | ##' @return A data frame containing the computed growth rate. 15 | ##' @export 16 | ##' @examples 17 | ##' test.df2 = data.frame(FAOST_CODE = rep(c(1, 5000), each = 5), 18 | ##' Year = rep(1990:1994, 2), 19 | ##' a = rep(1:5, 2), b = rep(1:5, 2)) 20 | ##' grConstruct(test.df2, origVar = "a", type = "geo", n = 1) 21 | ##' grConstruct(test.df2, origVar = "a", type = "geo", n = 3) 22 | ##' grConstruct(test.df2, origVar = "a", type = "geo", n = 5) 23 | grConstruct = function(data, origVar, newVarName = NA, 24 | type = c("geo", "ls", "ch"), n = 1){ 25 | type = match.arg(type) 26 | tmp = arrange(subset(data, select = c("FAOST_CODE", "Year", origVar)), 27 | FAOST_CODE, Year) 28 | unqCountry = unique(tmp$FAOST_CODE) 29 | grVar = double() 30 | if(type == "ls"){ 31 | for(i in 1:length(unqCountry)){ 32 | tmpgr = 33 | lsgr(as.numeric(unlist(subset(tmp, 34 | select = origVar, 35 | subset = FAOST_CODE == unqCountry[i]))), n) 36 | grVar = c(grVar, tmpgr) 37 | } 38 | } else if(type == "geo"){ 39 | for(i in 1:length(unqCountry)){ 40 | tmpgr = 41 | geogr(as.numeric(unlist(subset(tmp, 42 | select = origVar, 43 | subset = FAOST_CODE == unqCountry[i]))), 44 | n) 45 | grVar = c(grVar, tmpgr) 46 | } 47 | } else if(type == "ch"){ 48 | for(i in 1:length(unqCountry)){ 49 | tmpgr = 50 | chgr(as.numeric(unlist(subset(tmp, 51 | select = origVar, 52 | subset = FAOST_CODE == unqCountry[i]))), 53 | n) 54 | grVar = c(grVar, tmpgr) 55 | } 56 | } 57 | gr.df = data.frame(tmp[c("FAOST_CODE", "Year")], grVar) 58 | colnames(gr.df)[3] = ifelse(is.na(newVarName), 59 | paste(origVar, ".GR", n, sep = ""), 60 | newVarName) 61 | gr.df 62 | } 63 | 64 | utils::globalVariables(names = "FAOST_CODE") 65 | -------------------------------------------------------------------------------- /FAOSTAT/R/grConstruct.R: -------------------------------------------------------------------------------- 1 | ##' Construct Growth rate 2 | ##' 3 | ##' A function for constructing growth rate variables. 4 | ##' 5 | ##' @param data The data frame containing the data 6 | ##' @param origVar The variable in which the growth is to be calculated 7 | ##' @param newVarName The name assigned to the new variable, if missing 8 | ##' then .SC/.SH/.GR will be appended depending on the type of 9 | ##' construction. 10 | ##' @param type The type of growth rate, can be least squares or 11 | ##' geometric 12 | ##' @param n The period for the growth rate to be calculated (Refer to 13 | ##' the lsgr or the geogr functions.) 14 | ##' @return A data frame containing the computed growth rate. 15 | ##' @export 16 | ##' @examples 17 | ##' test.df2 = data.frame(FAOST_CODE = rep(c(1, 5000), each = 5), 18 | ##' Year = rep(1990:1994, 2), 19 | ##' a = rep(1:5, 2), b = rep(1:5, 2)) 20 | ##' grConstruct(test.df2, origVar = "a", type = "geo", n = 1) 21 | ##' grConstruct(test.df2, origVar = "a", type = "geo", n = 3) 22 | ##' grConstruct(test.df2, origVar = "a", type = "geo", n = 5) 23 | grConstruct = function(data, origVar, newVarName = NA, 24 | type = c("geo", "ls", "ch"), n = 1){ 25 | type = match.arg(type) 26 | tmp = arrange(subset(data, select = c("FAOST_CODE", "Year", origVar)), 27 | FAOST_CODE, Year) 28 | unqCountry = unique(tmp$FAOST_CODE) 29 | grVar = double() 30 | if(type == "ls"){ 31 | for(i in 1:length(unqCountry)){ 32 | tmpgr = 33 | lsgr(as.numeric(unlist(subset(tmp, 34 | select = origVar, 35 | subset = FAOST_CODE == unqCountry[i]))), n) 36 | grVar = c(grVar, tmpgr) 37 | } 38 | } else if(type == "geo"){ 39 | for(i in 1:length(unqCountry)){ 40 | tmpgr = 41 | geogr(as.numeric(unlist(subset(tmp, 42 | select = origVar, 43 | subset = FAOST_CODE == unqCountry[i]))), 44 | n) 45 | grVar = c(grVar, tmpgr) 46 | } 47 | } else if(type == "ch"){ 48 | for(i in 1:length(unqCountry)){ 49 | tmpgr = 50 | chgr(as.numeric(unlist(subset(tmp, 51 | select = origVar, 52 | subset = FAOST_CODE == unqCountry[i]))), 53 | n) 54 | grVar = c(grVar, tmpgr) 55 | } 56 | } 57 | gr.df = data.frame(tmp[c("FAOST_CODE", "Year")], grVar) 58 | colnames(gr.df)[3] = ifelse(is.na(newVarName), 59 | paste(origVar, ".GR", n, sep = ""), 60 | newVarName) 61 | gr.df 62 | } 63 | 64 | utils::globalVariables(names = "FAOST_CODE") 65 | -------------------------------------------------------------------------------- /Codes/R/lsgr.R: -------------------------------------------------------------------------------- 1 | ##' Least squares growth rate 2 | ##' 3 | ##' Function for generating the n-period rolling least squares growth 4 | ##' rate. 5 | ##' 6 | ##' Missing values are ommited in the regression. (Will need to check 7 | ##' this.) 8 | ##' 9 | ##' TODO (Michael): There is still some error associated with this 10 | ##' function, will need to investigate further. Will need a rule for 11 | ##' this, when the fluctuation is large and data are sufficient then 12 | ##' take the lsgr, otherwise the geogr. 13 | ##' 14 | ##' In order to ensure the growth rate calculated is reliable, the 15 | ##' following rule are applied. 16 | ##' \enumerate{ 17 | ##' \item 50\% of the data must be present. 18 | ##' \item The length of the time series must be greater than n. 19 | ##' } 20 | ##' Otherwise the growth will not be computed. 21 | ##' 22 | ##' @param x The time series for the growth rate to be calculated 23 | ##' @param n The period for the growth to be calculated over. 24 | ##' @return The n-period least squares growth rate of the time series 25 | ##' @export 26 | ##' @examples 27 | ##' test.ts = abs(rnorm(100)) 28 | ##' lsgr(test.ts, 1) 29 | ##' lsgr(test.ts, 3) 30 | ##' lsgr(test.ts, 10) 31 | ##' 32 | 33 | lsgr = function(x, n = 1){ 34 | T = length(x) 35 | if(all(is.na(x))){ 36 | lsgr = rep(NA, T) 37 | warning("All values are NA") 38 | } else { 39 | firstObs = min(which(!is.na(x))) 40 | if(length(na.omit(x)) < 5){ 41 | stop("Insufficient data for least squares growth rate, use other methods") 42 | } else { 43 | if(sum(is.na(x[firstObs:T])) > 0.5 * (T - firstObs + 1)){ 44 | t = 1:(n + 1) 45 | lsgr = double(T) 46 | lsgr[1:(firstObs + n - 1)] = NA 47 | for(i in (firstObs + n):T){ 48 | tmp = try((exp(coef(rlm(log(x[(i - n):(i)]) ~ t))[2]) - 1) * 49 | 100, silent = TRUE) 50 | if(!inherits(tmp, "try-error")){ 51 | lsgr[i] = tmp 52 | } else { 53 | lsgr[i] = NA 54 | } 55 | } 56 | warning("Over 50% of the data are missing, robust regression is used") 57 | } else { 58 | t = 1:(n + 1) 59 | lsgr = double(T) 60 | lsgr[1:(firstObs + n - 1)] = NA 61 | for(i in (firstObs + n):T){ 62 | tmp = try((exp(coef(lm(log(x[(i - n):(i)]) ~ t))[2]) - 1) * 100, 63 | silent = TRUE) 64 | if(!inherits(tmp, "try-error")){ 65 | lsgr[i] = tmp 66 | } else { 67 | lsgr[i] = NA 68 | } 69 | } 70 | } 71 | } 72 | } 73 | lsgr 74 | } 75 | -------------------------------------------------------------------------------- /FAOSTAT/R/lsgr.R: -------------------------------------------------------------------------------- 1 | ##' Least squares growth rate 2 | ##' 3 | ##' Function for generating the n-period rolling least squares growth 4 | ##' rate. 5 | ##' 6 | ##' Missing values are ommited in the regression. (Will need to check 7 | ##' this.) 8 | ##' 9 | ##' TODO (Michael): There is still some error associated with this 10 | ##' function, will need to investigate further. Will need a rule for 11 | ##' this, when the fluctuation is large and data are sufficient then 12 | ##' take the lsgr, otherwise the geogr. 13 | ##' 14 | ##' In order to ensure the growth rate calculated is reliable, the 15 | ##' following rule are applied. 16 | ##' \enumerate{ 17 | ##' \item 50\% of the data must be present. 18 | ##' \item The length of the time series must be greater than n. 19 | ##' } 20 | ##' Otherwise the growth will not be computed. 21 | ##' 22 | ##' @param x The time series for the growth rate to be calculated 23 | ##' @param n The period for the growth to be calculated over. 24 | ##' @return The n-period least squares growth rate of the time series 25 | ##' @export 26 | ##' @examples 27 | ##' test.ts = abs(rnorm(100)) 28 | ##' lsgr(test.ts, 1) 29 | ##' lsgr(test.ts, 3) 30 | ##' lsgr(test.ts, 10) 31 | ##' 32 | 33 | lsgr = function(x, n = 1){ 34 | T = length(x) 35 | if(all(is.na(x))){ 36 | lsgr = rep(NA, T) 37 | warning("All values are NA") 38 | } else { 39 | firstObs = min(which(!is.na(x))) 40 | if(length(na.omit(x)) < 5){ 41 | stop("Insufficient data for least squares growth rate, use other methods") 42 | } else { 43 | if(sum(is.na(x[firstObs:T])) > 0.5 * (T - firstObs + 1)){ 44 | t = 1:(n + 1) 45 | lsgr = double(T) 46 | lsgr[1:(firstObs + n - 1)] = NA 47 | for(i in (firstObs + n):T){ 48 | tmp = try((exp(coef(rlm(log(x[(i - n):(i)]) ~ t))[2]) - 1) * 49 | 100, silent = TRUE) 50 | if(!inherits(tmp, "try-error")){ 51 | lsgr[i] = tmp 52 | } else { 53 | lsgr[i] = NA 54 | } 55 | } 56 | warning("Over 50% of the data are missing, robust regression is used") 57 | } else { 58 | t = 1:(n + 1) 59 | lsgr = double(T) 60 | lsgr[1:(firstObs + n - 1)] = NA 61 | for(i in (firstObs + n):T){ 62 | tmp = try((exp(coef(lm(log(x[(i - n):(i)]) ~ t))[2]) - 1) * 100, 63 | silent = TRUE) 64 | if(!inherits(tmp, "try-error")){ 65 | lsgr[i] = tmp 66 | } else { 67 | lsgr[i] = NA 68 | } 69 | } 70 | } 71 | } 72 | } 73 | lsgr 74 | } 75 | -------------------------------------------------------------------------------- /FAOSTAT/man/Aggregation.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Aggregation.R 3 | \name{Aggregation} 4 | \alias{Aggregation} 5 | \title{Compute Aggregates} 6 | \usage{ 7 | Aggregation( 8 | data, 9 | aggVar, 10 | weightVar = rep(NA, length(aggVar)), 11 | year = "Year", 12 | relationDF = FAOcountryProfile[, c("FAOST_CODE", "M49_FAOST_CODE")], 13 | aggMethod = rep("sum", length(aggVar)), 14 | applyRules = TRUE, 15 | keepUnspecified = TRUE, 16 | unspecifiedCode = 0, 17 | thresholdProp = rep(0.65, length(aggVar)) 18 | ) 19 | } 20 | \arguments{ 21 | \item{data}{The data frame containing the country level data.} 22 | 23 | \item{aggVar}{The vector of names of the variables to be 24 | aggregated.} 25 | 26 | \item{weightVar}{The vector of names of the variables to be used 27 | as weighting when the aggregation method is weighted.} 28 | 29 | \item{year}{The column containing the time information.} 30 | 31 | \item{relationDF}{A relational data frame which specifies the 32 | territory and the mother country. At least one column must have 33 | a corrispondent variable name in the dataset.} 34 | 35 | \item{aggMethod}{Can be a single method for all data or a vector 36 | specifying different method for each variable. The method can be 37 | "sum", "mean", "weighted.mean".} 38 | 39 | \item{applyRules}{Logical, specifies whether the 40 | \code{thresholdProp} rule must be applied or not.} 41 | 42 | \item{keepUnspecified}{Whether countries with unspecified region 43 | should be aggregated into an "Unspecified" group or simply 44 | drop. Default to create the new group.} 45 | 46 | \item{unspecifiedCode}{The output code of the unspecified group.} 47 | 48 | \item{thresholdProp}{The vector of the missing threshold for the 49 | aggregation rule to be applied. The default is set to only compute 50 | aggregation if there are more than 65 percent of data available (0.65).} 51 | } 52 | \description{ 53 | The function takes a relational data frame and computes the aggregation 54 | based on the relation specified. 55 | } 56 | \details{ 57 | The length of \code{aggVar}, \code{aggMethod}, \code{weightVar}, 58 | \code{thresholdProp} must be the same. 59 | 60 | Aggregation should not be computed if insufficient 61 | countries have reported data. This corresponds to the argument 62 | \code{thresholdProp} which specifies the percentage which of 63 | country must report data (both for the variable to be aggregated and 64 | the weighting variable). 65 | } 66 | \examples{ 67 | ## example.df = data.frame(FAOST_CODE = rep(c(1, 2, 3), 2), 68 | ## Year = rep(c(2010, 2011), c(3, 3)), 69 | ## value = rep(c(1, 2, 3), 2), 70 | ## weight = rep(c(0.3, 0.7, 1), 2)) 71 | 72 | ## Lets aggregate country 1 and 2 into one country and keep country 73 | ## 3 seperate. 74 | ## relation.df = data.frame(FAOST_CODE = 1:3, NEW_CODE = c(1, 1, 2)) 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Codes/R/shConstruct.R: -------------------------------------------------------------------------------- 1 | ##' Construct share variable 2 | ##' 3 | ##' A function for constructing the share of a variable of an aggregated 4 | ##' variable. 5 | ##' 6 | ##' The share of a variable can be share of the World (if additional 7 | ##' variable were not supplied) or share of another variable (per Capita 8 | ##' if population was supplied). 9 | ##' 10 | ##' @param data The data frame containing both the share variable and 11 | ##' the aggregated variable 12 | ##' @param totVar The aggregated variable. 13 | ##' @param shareVar The subset of the aggregated variable which to be 14 | ##' divided by. 15 | ##' @param newVarName The name assigned to the new variable, if missing 16 | ##' then .SC/.SH/.GR will be appended depending on the type of 17 | ##' construction 18 | ##' @return A data frame with the new constructed variable 19 | ##' @export 20 | ##' @examples 21 | ##' ## Total variables provided, scale by totVar 22 | ##' test.df = data.frame(FAOST_CODE = 1, Year = 1990:1994, a = 1:5, b = 1:5) 23 | ##' shConstruct(data = test.df, totVar = "a", shareVar = "b") 24 | ##' 25 | ##' ## Total variables not provided, scale by world aggregate. 26 | ##' test.df2 = data.frame(FAOST_CODE = rep(c(1, 5000), each = 5), 27 | ##' Year = rep(1990:1994, 2), 28 | ##' a = rep(1:5, 2), b = rep(1:5, 2)) 29 | ##' shConstruct(data = test.df2, totVar = NA, shareVar = "b") 30 | shConstruct = function(data, totVar, shareVar, newVarName = NA){ 31 | data = arrange(data, FAOST_CODE, Year) 32 | if(is.na(totVar)){ 33 | shareData = subset(data, select = c("FAOST_CODE", "Year", shareVar)) 34 | ## world.df = subset(data, subset = FAOST_CODE == 5000, 35 | ## select = c("Year", shareVar)) 36 | ## colnames(world.df) = c("Year", "World") 37 | ## data = arrange(merge(data, world.df), FAOST_CODE, Year) 38 | ## totVar = "World" 39 | 40 | world.df = ddply(.data = shareData, .variables = .(Year), 41 | .fun = function(x) sum(x[[shareVar]], na.rm = TRUE)) 42 | colnames(world.df) = c("Year", "World") 43 | fd = merge(shareData, world.df, by = "Year") 44 | newVarName = ifelse(is.na(newVarName), paste(shareVar, ".SC", sep = ""), 45 | newVarName) 46 | fd[[newVarName]] = fd[[shareVar]]/fd[["World"]] 47 | shVar.df = subset(fd, select = c("FAOST_CODE", "Year", newVarName)) 48 | 49 | ## shVar = data[shareVar]/data[totVar] 50 | ## colnames(shVar) = ifelse(is.na(newVarName), 51 | ## paste(shareVar, ".SC", sep = ""), 52 | ## newVarName) 53 | } else { 54 | ## I don't think we should multiply by 100 55 | shareData = subset(data, select = c("FAOST_CODE", "Year", shareVar, totVar)) 56 | newVarName = ifelse(is.na(newVarName), paste(shareVar, ".SH", sep = ""), 57 | newVarName) 58 | shareData[[newVarName]] = data[[shareVar]]/data[[totVar]] 59 | shVar.df = subset(shareData, select = c("FAOST_CODE", "Year", newVarName)) 60 | 61 | } 62 | arrange(shVar.df, FAOST_CODE, Year) 63 | ## merge(data, shVar.df, by = c("FAOST_CODE", "Year")) 64 | ## data.frame(data[c("FAOST_CODE", "Year")], shVar) 65 | } 66 | 67 | utils::globalVariables(names = "FAOST_CODE") 68 | -------------------------------------------------------------------------------- /FAOSTAT/R/shConstruct.R: -------------------------------------------------------------------------------- 1 | ##' Construct share variable 2 | ##' 3 | ##' A function for constructing the share of a variable of an aggregated 4 | ##' variable. 5 | ##' 6 | ##' The share of a variable can be share of the World (if additional 7 | ##' variable were not supplied) or share of another variable (per Capita 8 | ##' if population was supplied). 9 | ##' 10 | ##' @param data The data frame containing both the share variable and 11 | ##' the aggregated variable 12 | ##' @param totVar The aggregated variable. 13 | ##' @param shareVar The subset of the aggregated variable which to be 14 | ##' divided by. 15 | ##' @param newVarName The name assigned to the new variable, if missing 16 | ##' then .SC/.SH/.GR will be appended depending on the type of 17 | ##' construction 18 | ##' @return A data frame with the new constructed variable 19 | ##' @export 20 | ##' @examples 21 | ##' ## Total variables provided, scale by totVar 22 | ##' test.df = data.frame(FAOST_CODE = 1, Year = 1990:1994, a = 1:5, b = 1:5) 23 | ##' shConstruct(data = test.df, totVar = "a", shareVar = "b") 24 | ##' 25 | ##' ## Total variables not provided, scale by world aggregate. 26 | ##' test.df2 = data.frame(FAOST_CODE = rep(c(1, 5000), each = 5), 27 | ##' Year = rep(1990:1994, 2), 28 | ##' a = rep(1:5, 2), b = rep(1:5, 2)) 29 | ##' shConstruct(data = test.df2, totVar = NA, shareVar = "b") 30 | shConstruct = function(data, totVar, shareVar, newVarName = NA){ 31 | data = arrange(data, FAOST_CODE, Year) 32 | if(is.na(totVar)){ 33 | shareData = subset(data, select = c("FAOST_CODE", "Year", shareVar)) 34 | ## world.df = subset(data, subset = FAOST_CODE == 5000, 35 | ## select = c("Year", shareVar)) 36 | ## colnames(world.df) = c("Year", "World") 37 | ## data = arrange(merge(data, world.df), FAOST_CODE, Year) 38 | ## totVar = "World" 39 | 40 | world.df = ddply(.data = shareData, .variables = .(Year), 41 | .fun = function(x) sum(x[[shareVar]], na.rm = TRUE)) 42 | colnames(world.df) = c("Year", "World") 43 | fd = merge(shareData, world.df, by = "Year") 44 | newVarName = ifelse(is.na(newVarName), paste(shareVar, ".SC", sep = ""), 45 | newVarName) 46 | fd[[newVarName]] = fd[[shareVar]]/fd[["World"]] 47 | shVar.df = subset(fd, select = c("FAOST_CODE", "Year", newVarName)) 48 | 49 | ## shVar = data[shareVar]/data[totVar] 50 | ## colnames(shVar) = ifelse(is.na(newVarName), 51 | ## paste(shareVar, ".SC", sep = ""), 52 | ## newVarName) 53 | } else { 54 | ## I don't think we should multiply by 100 55 | shareData = subset(data, select = c("FAOST_CODE", "Year", shareVar, totVar)) 56 | newVarName = ifelse(is.na(newVarName), paste(shareVar, ".SH", sep = ""), 57 | newVarName) 58 | shareData[[newVarName]] = data[[shareVar]]/data[[totVar]] 59 | shVar.df = subset(shareData, select = c("FAOST_CODE", "Year", newVarName)) 60 | 61 | } 62 | arrange(shVar.df, FAOST_CODE, Year) 63 | ## merge(data, shVar.df, by = c("FAOST_CODE", "Year")) 64 | ## data.frame(data[c("FAOST_CODE", "Year")], shVar) 65 | } 66 | 67 | utils::globalVariables(names = "FAOST_CODE") 68 | -------------------------------------------------------------------------------- /Codes/R/getWDImetaData.R: -------------------------------------------------------------------------------- 1 | ##' World Bank Indicator Metadata 2 | ##' 3 | ##' A function to extract the definition and the meta data from the 4 | ##' World Bank API 5 | ##' 6 | ##' @param indicator The World Bank official indicator name. 7 | ##' @param printMetaData logical, print out the meta data information 8 | ##' @param saveMetaData logical, whether meta data should be saved as a 9 | ##' local csv file. 10 | ##' @param saveName The name of the file for the meta data to save to. 11 | ##' @export 12 | ##' @examples 13 | ##' ## pop.df = getWDImetaData("SP.POP.TOTL", 14 | ##' ## printMetaData = TRUE, saveMetaData = TRUE) 15 | ##' 16 | 17 | 18 | getWDImetaData = function(indicator, printMetaData = FALSE, 19 | saveMetaData = FALSE, 20 | saveName = "worldBankMetaData"){ 21 | indicator = toupper(indicator) 22 | url = paste("http://api.worldbank.org/indicators/", indicator, 23 | "?format=json", sep = "") 24 | base = data.frame(indicator = character(0), 25 | id = character(0), 26 | name = character(0), 27 | source = character(0), 28 | sourceNote = character(0), 29 | sourceOrganization = character(0), 30 | ## topics1 = character(0), 31 | ## topics2 = character(0), 32 | stringsAsFactors = FALSE) 33 | for(i in 1:length(url)){ 34 | tmp = try(fromJSON(url[i])) 35 | if(!inherits(tmp, "try-error")){ 36 | if(length(tmp[[2]]) != 0){ 37 | base[i, ] = c(indicator[i], 38 | tmp[[2]][[1]]$id, 39 | tmp[[2]][[1]]$name, 40 | tmp[[2]][[1]]$source["value"], 41 | tmp[[2]][[1]]$sourceNote, 42 | tmp[[2]][[1]]$sourceOrganization) 43 | ## ifelse(length(tmp[[2]][[1]]) == 1, 44 | ## tmp[[2]][[1]]$topics[[1]]["value"], NA), 45 | ## ifelse(length(tmp[[2]][[1]]) == 2, 46 | ## tmp[[2]][[1]]$topics[[2]]["value"], NA)) 47 | } 48 | if(printMetaData){ 49 | printLab(indicator[i], span = TRUE) 50 | cat("Name of Indicator:") 51 | cat(paste("\n\t", base[i, ]$name, "\n", sep = "")) 52 | cat("Source:") 53 | cat(paste("\n\t", base[i, ]$source, "\n", sep = "")) 54 | cat("Source Note:") 55 | cat(strwrap(base[i, ]$sourceNote, width = 60, prefix = "\n\t")) 56 | cat("\nSource Organization:") 57 | cat(strwrap(base[i, ]$sourceOrganization, width = 60, 58 | prefix = "\n\t")) 59 | ## cat("\nTopic(1):") 60 | ## cat(paste("\n\t", base[i, ]$topics1, "\n", sep = "")) 61 | ## cat("Topic(2):") 62 | ## cat(paste("\n\t", base[i, ]$topics2, "\n", sep = "")) 63 | } 64 | } 65 | } 66 | if(saveMetaData){ 67 | write.csv(base, file = paste(saveName, ".csv", sep = ""), 68 | row.names = FALSE) 69 | cat(paste("\n\n** Output written to '", saveName, ".csv'\n", sep = "")) 70 | } 71 | base 72 | } 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /FAOSTAT/R/getWDImetaData.R: -------------------------------------------------------------------------------- 1 | ##' World Bank Indicator Metadata 2 | ##' 3 | ##' A function to extract the definition and the meta data from the 4 | ##' World Bank API 5 | ##' 6 | ##' @param indicator The World Bank official indicator name. 7 | ##' @param printMetaData logical, print out the meta data information 8 | ##' @param saveMetaData logical, whether meta data should be saved as a 9 | ##' local csv file. 10 | ##' @param saveName The name of the file for the meta data to save to. 11 | ##' @export 12 | ##' @examples 13 | ##' ## pop.df = getWDImetaData("SP.POP.TOTL", 14 | ##' ## printMetaData = TRUE, saveMetaData = TRUE) 15 | ##' 16 | 17 | 18 | getWDImetaData = function(indicator, printMetaData = FALSE, 19 | saveMetaData = FALSE, 20 | saveName = "worldBankMetaData"){ 21 | indicator = toupper(indicator) 22 | url = paste("http://api.worldbank.org/indicators/", indicator, 23 | "?format=json", sep = "") 24 | base = data.frame(indicator = character(0), 25 | id = character(0), 26 | name = character(0), 27 | source = character(0), 28 | sourceNote = character(0), 29 | sourceOrganization = character(0), 30 | ## topics1 = character(0), 31 | ## topics2 = character(0), 32 | stringsAsFactors = FALSE) 33 | for(i in 1:length(url)){ 34 | tmp = try(fromJSON(url[i])) 35 | if(!inherits(tmp, "try-error")){ 36 | if(length(tmp[[2]]) != 0){ 37 | base[i, ] = c(indicator[i], 38 | tmp[[2]][[1]]$id, 39 | tmp[[2]][[1]]$name, 40 | tmp[[2]][[1]]$source["value"], 41 | tmp[[2]][[1]]$sourceNote, 42 | tmp[[2]][[1]]$sourceOrganization) 43 | ## ifelse(length(tmp[[2]][[1]]) == 1, 44 | ## tmp[[2]][[1]]$topics[[1]]["value"], NA), 45 | ## ifelse(length(tmp[[2]][[1]]) == 2, 46 | ## tmp[[2]][[1]]$topics[[2]]["value"], NA)) 47 | } 48 | if(printMetaData){ 49 | printLab(indicator[i], span = TRUE) 50 | cat("Name of Indicator:") 51 | cat(paste("\n\t", base[i, ]$name, "\n", sep = "")) 52 | cat("Source:") 53 | cat(paste("\n\t", base[i, ]$source, "\n", sep = "")) 54 | cat("Source Note:") 55 | cat(strwrap(base[i, ]$sourceNote, width = 60, prefix = "\n\t")) 56 | cat("\nSource Organization:") 57 | cat(strwrap(base[i, ]$sourceOrganization, width = 60, 58 | prefix = "\n\t")) 59 | ## cat("\nTopic(1):") 60 | ## cat(paste("\n\t", base[i, ]$topics1, "\n", sep = "")) 61 | ## cat("Topic(2):") 62 | ## cat(paste("\n\t", base[i, ]$topics2, "\n", sep = "")) 63 | } 64 | } 65 | } 66 | if(saveMetaData){ 67 | write.csv(base, file = paste(saveName, ".csv", sep = ""), 68 | row.names = FALSE) 69 | cat(paste("\n\n** Output written to '", saveName, ".csv'\n", sep = "")) 70 | } 71 | base 72 | } 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /Codes/R/FAOcheck.R: -------------------------------------------------------------------------------- 1 | ##' This function perform some check on the data 2 | ##' 3 | ##' The function only works for FAOST_CODE. If the country coding 4 | ##' system is not in FAOST_CODE then use the translateCountryCode 5 | ##' function to translate it. 6 | ##' 7 | ##' @param var The variable to be checked. 8 | ##' @param year The column which index the time. 9 | ##' @param data The data frame. 10 | ##' @param type The type of check. 11 | ##' @param take The type of check/replacement to be done in case of 12 | ##' type equals to overlap. 13 | ##' 14 | ##' @export 15 | ##' @examples 16 | ##' ## test.df = 17 | ##' ## data.frame(FAOST_CODE = rep(c(51,167,199), each = 3), 18 | ##' ## Year = rep(c(1990:1992), 3), 19 | ##' ## Value = c(c(3,4,4), c(2,2,2), c(1,2,NA))) 20 | ##' ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "simpleCheck") 21 | ##' ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "takeNew") 22 | ##' ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "takeOld") 23 | ##' ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "complete") 24 | 25 | FAOcheck = function(var, year = "Year", data, 26 | type = c("overlap", "multiChina"), 27 | take = c("simpleCheck", "takeNew", "takeOld", "complete")){ 28 | type = match.arg(type) 29 | take = match.arg(take) 30 | if(type == "overlap"){ 31 | printLab("Check for overlap between transitional country") 32 | ## Belgium-Luxembourg (old entity, up to 1999 included) vs 33 | ## Belgium, LuxemBourg (new entities, from 2000). 34 | data = overlap(old = 15, new = c(255, 256), 35 | var = var, data = data, take = take) 36 | ## Czechoslovakia (old entity, up to 1992 included) vs 37 | ## Czech Republic, Slovakia (new entities, from 1993). 38 | data = overlap(old = 51, new = c(167, 199), 39 | var = var, data = data, take = take) 40 | ## Ethiopia PDR (old entity, up to 1992 included) vs 41 | ## Eritrea, Ethiopia (new entities, from 1993). 42 | data = overlap(old = 62, new = c(178, 238), 43 | var = var, data = data, take = take) 44 | ## Serbia and Montenegro (old entity, up to 2005 included) vs 45 | ## Serbia, Montenegro (new entities, from 2006). 46 | data = overlap(old = 186, new = c(272, 273), 47 | var = var, data = data, take = take) 48 | ## USSR (old entity, up to 1991 included) vs 49 | ## Armenia, Azerbaijan, Belarus, Estonia, Georgia, Kazakistan 50 | ## Kyrghisistan, Latvia, Lithuania, Moldova, Russian Federation, 51 | ## Tajikistan, Turkmenistan, Ukraine, Uzbekistan (new entities, from 1992). 52 | data = overlap(old = 228, new = c(1,52,57,63,73,108,113,119,126,146,185,208,213,230,235), 53 | var = var, data = data, take = take) 54 | ## Yemen (old entities, up to ...) vs 55 | ## Yemen (new entities, from ...) 56 | data = overlap(old = c(246, 247), new = 249, 57 | var = var, data = data, take = take) 58 | ## Yugoslav SFR (old entity, up to 1991 included) vs 59 | ## Serbia e Montenegro, Bosnia and Herzegovina, Croatia, 60 | ## Slovenia, Macedonia (new entities, from 1992). 61 | data = overlap(old = 248, new = c(80, 98, 154, 186, 272, 273, 198), 62 | var = var, data = data, take = take) 63 | ## Sudan (former) (old entity, up to 2011 included) vs 64 | ## Sudan, South Sudan (new entities, from 2012). 65 | data = overlap(old = 206, new = c(276, 277), 66 | var = var, data = data, take = take) 67 | ## Netherlands Antilles (old entity, up to 2010 included) vs 68 | ## Aruba, Bonaire, Sint Eustatius and Saba, Curacao, 69 | ## Sint Maarten (Dutch Part) (new entities, from 2011). 70 | data = overlap(old = 151, new = c(22,278,279,280), 71 | var = var, data = data, take = take) 72 | ## Pacific Islands, Trust Territory of (old entity, up to 1990 included) vs 73 | ## Micronesia (Federated States of), the Marshall Islands, 74 | ## Northern Mariana Islands, Palau (new entities, from 1991). 75 | data = overlap(old = 164, new = c(145,127,163,180), 76 | var = var, data = data, take = take) 77 | cat("\nNOTE: It is common for data reported by the predecessor\n or the new transitional country to include the new country\n") 78 | } 79 | if(type == "multiChina"){ 80 | printLab("Check for existence of multiple China") 81 | data = CHMT(var = var, data = data, year = year) 82 | } 83 | data 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /FAOSTAT/R/FAOcheck.R: -------------------------------------------------------------------------------- 1 | ##' This function perform some check on the data 2 | ##' 3 | ##' The function only works for FAOST_CODE. If the country coding 4 | ##' system is not in FAOST_CODE then use the translateCountryCode 5 | ##' function to translate it. 6 | ##' 7 | ##' @param var The variable to be checked. 8 | ##' @param year The column which index the time. 9 | ##' @param data The data frame. 10 | ##' @param type The type of check. 11 | ##' @param take The type of check/replacement to be done in case of 12 | ##' type equals to overlap. 13 | ##' 14 | ##' @export 15 | ##' @examples 16 | ##' ## test.df = 17 | ##' ## data.frame(FAOST_CODE = rep(c(51,167,199), each = 3), 18 | ##' ## Year = rep(c(1990:1992), 3), 19 | ##' ## Value = c(c(3,4,4), c(2,2,2), c(1,2,NA))) 20 | ##' ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "simpleCheck") 21 | ##' ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "takeNew") 22 | ##' ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "takeOld") 23 | ##' ## FAOcheck(var = "Value", data = test.df, type = "overlap", take = "complete") 24 | 25 | FAOcheck = function(var, year = "Year", data, 26 | type = c("overlap", "multiChina"), 27 | take = c("simpleCheck", "takeNew", "takeOld", "complete")){ 28 | type = match.arg(type) 29 | take = match.arg(take) 30 | if(type == "overlap"){ 31 | printLab("Check for overlap between transitional country") 32 | ## Belgium-Luxembourg (old entity, up to 1999 included) vs 33 | ## Belgium, LuxemBourg (new entities, from 2000). 34 | data = overlap(old = 15, new = c(255, 256), 35 | var = var, data = data, take = take) 36 | ## Czechoslovakia (old entity, up to 1992 included) vs 37 | ## Czech Republic, Slovakia (new entities, from 1993). 38 | data = overlap(old = 51, new = c(167, 199), 39 | var = var, data = data, take = take) 40 | ## Ethiopia PDR (old entity, up to 1992 included) vs 41 | ## Eritrea, Ethiopia (new entities, from 1993). 42 | data = overlap(old = 62, new = c(178, 238), 43 | var = var, data = data, take = take) 44 | ## Serbia and Montenegro (old entity, up to 2005 included) vs 45 | ## Serbia, Montenegro (new entities, from 2006). 46 | data = overlap(old = 186, new = c(272, 273), 47 | var = var, data = data, take = take) 48 | ## USSR (old entity, up to 1991 included) vs 49 | ## Armenia, Azerbaijan, Belarus, Estonia, Georgia, Kazakistan 50 | ## Kyrghisistan, Latvia, Lithuania, Moldova, Russian Federation, 51 | ## Tajikistan, Turkmenistan, Ukraine, Uzbekistan (new entities, from 1992). 52 | data = overlap(old = 228, new = c(1,52,57,63,73,108,113,119,126,146,185,208,213,230,235), 53 | var = var, data = data, take = take) 54 | ## Yemen (old entities, up to ...) vs 55 | ## Yemen (new entities, from ...) 56 | data = overlap(old = c(246, 247), new = 249, 57 | var = var, data = data, take = take) 58 | ## Yugoslav SFR (old entity, up to 1991 included) vs 59 | ## Serbia e Montenegro, Bosnia and Herzegovina, Croatia, 60 | ## Slovenia, Macedonia (new entities, from 1992). 61 | data = overlap(old = 248, new = c(80, 98, 154, 186, 272, 273, 198), 62 | var = var, data = data, take = take) 63 | ## Sudan (former) (old entity, up to 2011 included) vs 64 | ## Sudan, South Sudan (new entities, from 2012). 65 | data = overlap(old = 206, new = c(276, 277), 66 | var = var, data = data, take = take) 67 | ## Netherlands Antilles (old entity, up to 2010 included) vs 68 | ## Aruba, Bonaire, Sint Eustatius and Saba, Curacao, 69 | ## Sint Maarten (Dutch Part) (new entities, from 2011). 70 | data = overlap(old = 151, new = c(22,278,279,280), 71 | var = var, data = data, take = take) 72 | ## Pacific Islands, Trust Territory of (old entity, up to 1990 included) vs 73 | ## Micronesia (Federated States of), the Marshall Islands, 74 | ## Northern Mariana Islands, Palau (new entities, from 1991). 75 | data = overlap(old = 164, new = c(145,127,163,180), 76 | var = var, data = data, take = take) 77 | cat("\nNOTE: It is common for data reported by the predecessor\n or the new transitional country to include the new country\n") 78 | } 79 | if(type == "multiChina"){ 80 | printLab("Check for existence of multiple China") 81 | data = CHMT(var = var, data = data, year = year) 82 | } 83 | data 84 | } 85 | 86 | 87 | -------------------------------------------------------------------------------- /Codes/R/FAOsearch.R: -------------------------------------------------------------------------------- 1 | ##' A function to find the domain, element and item code for a 2 | ##' specific FAOSTAT query. 3 | ##' 4 | ##' @export 5 | 6 | FAOsearch = function(){ 7 | with(FAOmetaTable, { 8 | ## Find the Group code 9 | gc = NA 10 | ## while loop iterates until a valid value is supplied 11 | while(length(gc)==0 || is.na(gc)){ 12 | cat(paste(paste("(", 1:length(groupTable$groupName), ") ", 13 | groupTable$groupName, sep = ""), collapse = "\n")) 14 | gcn = readline("\nWhich Group are you looking for: ") 15 | gc = groupTable[as.numeric(gcn), "groupCode"] 16 | } 17 | 18 | ## Find the Domain code 19 | subdomainTable = subset(domainTable, groupCode == gc) 20 | dc = NA 21 | ## while loop iterates until a valid value is supplied 22 | while(length(dc)==0 || is.na(dc)){ 23 | cat(paste(paste("(", 1:length(subdomainTable$domainName), ") ", 24 | subdomainTable$domainName, sep = ""), 25 | collapse = "\n")) 26 | dcn = readline("\nWhich Domain are you looking for: ") 27 | dc = subdomainTable[as.numeric(dcn), "domainCode"] 28 | } 29 | 30 | ## Individual or aggregated item 31 | useAgg = NA 32 | while(is.na(useAgg) || !useAgg %in% c("0", "1")){ 33 | cat("(0) Individual item (e.g. Apples, Wheat)\n") 34 | cat("(1) Aggregated item (e.g. Total cereals, Total meat\n") 35 | useAgg = readline(paste("Are you looking for individual item or", 36 | "aggregated item:")) 37 | } 38 | 39 | if(as.numeric(useAgg)){ 40 | ## Find the Item Aggregated code 41 | subitemTable = subset(itemAggTable, domainCode == dc) 42 | ic = NA 43 | ## while loop iterates until a valid value is supplied 44 | while(length(ic)==0 || is.na(ic)){ 45 | cat(paste(paste("(", 1:length(subitemTable$itemName), ") ", 46 | subitemTable$itemName, sep = ""), 47 | collapse = "\n")) 48 | icn = readline(paste("\nWhich Item are you looking for?", 49 | "('All' for everything):")) 50 | if(icn == "All") 51 | icn = 1:length(subitemTable$itemName) 52 | ic = subitemTable[as.numeric(icn), "itemCode"] 53 | } 54 | } else { 55 | 56 | ## Find the Item code 57 | subitemTable = subset(itemTable, domainCode == dc) 58 | ic = NA 59 | ## while loop iterates until a valid value is supplied 60 | while(length(ic)==0 || is.na(ic)){ 61 | cat(paste(paste("(", 1:length(subitemTable$itemName), ") ", 62 | subitemTable$itemName, sep = ""), 63 | collapse = "\n")) 64 | icn = readline(paste("\nWhich Item are you looking for?", 65 | "('All' for everything): ")) 66 | if(icn == "All") 67 | icn = 1:length(subitemTable$itemName) 68 | ic = subitemTable[as.numeric(icn), "itemCode"] 69 | } 70 | } 71 | 72 | ## Find the Element code 73 | subelementTable = subset(elementTable, domainCode == dc) 74 | ec = NA 75 | ## while loop iterates until a valid value is supplied 76 | while(length(ec)==0 || is.na(ec)){ 77 | cat(paste(paste("(", 1:length(subelementTable$elementName), ") ", 78 | subelementTable$elementName, sep = ""), 79 | collapse = "\n")) 80 | ecn = readline(paste("\nWhich Element are you looking for?", 81 | "('All' for everything):")) 82 | if(ecn == "All") 83 | ecn = 1:length(subelementTable$elementName) 84 | ec = subelementTable[as.numeric(ecn), "elementCode"] 85 | } 86 | 87 | tmp = expand.grid(dc, ic, ec, stringsAsFactors = FALSE) 88 | colnames(tmp) = c("domainCode", "itemCode", "elementCode") 89 | tmp = merge(tmp, domainTable[, c("domainCode", "domainName")], 90 | all.x = TRUE) 91 | tmp = merge(tmp, subitemTable[, c("itemCode", "itemName")], 92 | all.x = TRUE) 93 | final.df = merge(tmp, subelementTable[, c("elementCode", "elementName")], 94 | all.x = TRUE) 95 | final.df$name = 96 | with(final.df, paste(domainName, itemName, elementName, sep = "_")) 97 | final.df$domainName = NULL 98 | final.df$itemName = NULL 99 | final.df$elementName = NULL 100 | .LastSearch <<- final.df 101 | cat("\n** Search result saved as .LastSearch**\n") 102 | 103 | } 104 | ) 105 | } 106 | 107 | utils::globalVariables(names = c("FAOmetaTable")) -------------------------------------------------------------------------------- /FAOSTAT/R/FAOsearch.R: -------------------------------------------------------------------------------- 1 | ##' A function to find the domain, element and item code for a 2 | ##' specific FAOSTAT query. 3 | ##' 4 | ##' @export 5 | 6 | FAOsearch = function(){ 7 | with(FAOmetaTable, { 8 | ## Find the Group code 9 | gc = NA 10 | ## while loop iterates until a valid value is supplied 11 | while(length(gc)==0 || is.na(gc)){ 12 | cat(paste(paste("(", 1:length(groupTable$groupName), ") ", 13 | groupTable$groupName, sep = ""), collapse = "\n")) 14 | gcn = readline("\nWhich Group are you looking for: ") 15 | gc = groupTable[as.numeric(gcn), "groupCode"] 16 | } 17 | 18 | ## Find the Domain code 19 | subdomainTable = subset(domainTable, groupCode == gc) 20 | dc = NA 21 | ## while loop iterates until a valid value is supplied 22 | while(length(dc)==0 || is.na(dc)){ 23 | cat(paste(paste("(", 1:length(subdomainTable$domainName), ") ", 24 | subdomainTable$domainName, sep = ""), 25 | collapse = "\n")) 26 | dcn = readline("\nWhich Domain are you looking for: ") 27 | dc = subdomainTable[as.numeric(dcn), "domainCode"] 28 | } 29 | 30 | ## Individual or aggregated item 31 | useAgg = NA 32 | while(is.na(useAgg) || !useAgg %in% c("0", "1")){ 33 | cat("(0) Individual item (e.g. Apples, Wheat)\n") 34 | cat("(1) Aggregated item (e.g. Total cereals, Total meat\n") 35 | useAgg = readline(paste("Are you looking for individual item or", 36 | "aggregated item:")) 37 | } 38 | 39 | if(as.numeric(useAgg)){ 40 | ## Find the Item Aggregated code 41 | subitemTable = subset(itemAggTable, domainCode == dc) 42 | ic = NA 43 | ## while loop iterates until a valid value is supplied 44 | while(length(ic)==0 || is.na(ic)){ 45 | cat(paste(paste("(", 1:length(subitemTable$itemName), ") ", 46 | subitemTable$itemName, sep = ""), 47 | collapse = "\n")) 48 | icn = readline(paste("\nWhich Item are you looking for?", 49 | "('All' for everything):")) 50 | if(icn == "All") 51 | icn = 1:length(subitemTable$itemName) 52 | ic = subitemTable[as.numeric(icn), "itemCode"] 53 | } 54 | } else { 55 | 56 | ## Find the Item code 57 | subitemTable = subset(itemTable, domainCode == dc) 58 | ic = NA 59 | ## while loop iterates until a valid value is supplied 60 | while(length(ic)==0 || is.na(ic)){ 61 | cat(paste(paste("(", 1:length(subitemTable$itemName), ") ", 62 | subitemTable$itemName, sep = ""), 63 | collapse = "\n")) 64 | icn = readline(paste("\nWhich Item are you looking for?", 65 | "('All' for everything): ")) 66 | if(icn == "All") 67 | icn = 1:length(subitemTable$itemName) 68 | ic = subitemTable[as.numeric(icn), "itemCode"] 69 | } 70 | } 71 | 72 | ## Find the Element code 73 | subelementTable = subset(elementTable, domainCode == dc) 74 | ec = NA 75 | ## while loop iterates until a valid value is supplied 76 | while(length(ec)==0 || is.na(ec)){ 77 | cat(paste(paste("(", 1:length(subelementTable$elementName), ") ", 78 | subelementTable$elementName, sep = ""), 79 | collapse = "\n")) 80 | ecn = readline(paste("\nWhich Element are you looking for?", 81 | "('All' for everything):")) 82 | if(ecn == "All") 83 | ecn = 1:length(subelementTable$elementName) 84 | ec = subelementTable[as.numeric(ecn), "elementCode"] 85 | } 86 | 87 | tmp = expand.grid(dc, ic, ec, stringsAsFactors = FALSE) 88 | colnames(tmp) = c("domainCode", "itemCode", "elementCode") 89 | tmp = merge(tmp, domainTable[, c("domainCode", "domainName")], 90 | all.x = TRUE) 91 | tmp = merge(tmp, subitemTable[, c("itemCode", "itemName")], 92 | all.x = TRUE) 93 | final.df = merge(tmp, subelementTable[, c("elementCode", "elementName")], 94 | all.x = TRUE) 95 | final.df$name = 96 | with(final.df, paste(domainName, itemName, elementName, sep = "_")) 97 | final.df$domainName = NULL 98 | final.df$itemName = NULL 99 | final.df$elementName = NULL 100 | .LastSearch <<- final.df 101 | cat("\n** Search result saved as .LastSearch**\n") 102 | 103 | } 104 | ) 105 | } 106 | 107 | utils::globalVariables(names = c("FAOmetaTable")) -------------------------------------------------------------------------------- /Codes/R/constructSYB.R: -------------------------------------------------------------------------------- 1 | ##' Construct/Creat new variable. 2 | ##' 3 | ##' A function used to construct new variables from existing variables. 4 | ##' 5 | ##' Currently two types of construction are supported, either share or 6 | ##' growth rate computation. 7 | ##' 8 | ##' Share can be a share of total or share of another variable depending 9 | ##' on whether an additional variable is supplied or not. 10 | ##' 11 | ##' @param data The data frame containing the raw variable 12 | ##' @param origVar1 The variable name to be used in construction, refer 13 | ##' to Details for more information and useage. 14 | ##' @param origVar2 The variable name to be used in construction, refer 15 | ##' to Details for more information and useage. 16 | ##' @param newVarName The name assigned to the new variable, if missing 17 | ##' then .SC/.SH/.GR/.CH will be appended depending on the type of 18 | ##' construction 19 | ##' @param constructType The type of construction, refer to Details 20 | ##' for more information. 21 | ##' @param grFreq The frequency for the growth rate to be computed. 22 | ##' @param grType The method for the growth to be calculated, currently 23 | ##' supports least squares and geometric. 24 | ##' @param baseYear The base year to be used for constructing index. 25 | ##' @export 26 | ##' @return A data frame containing both the original data frame and the 27 | ##' processed data and also a list indicating whether the construction 28 | ##' passed or failed. 29 | 30 | constructSYB = function(data, origVar1, origVar2, newVarName = NA, 31 | constructType = c("share", "growth", "change", "index"), 32 | grFreq = 1, grType = c("ls", "geo"), baseYear = 2000){ 33 | ## The length of the variables must be the same 34 | checkDim = try(data.frame(origVar1, origVar2, newVarName, 35 | constructType, grFreq, baseYear)) 36 | if(inherits(checkDim, "try-error")) 37 | stop("length of original variables are not the same") 38 | n = length(origVar1) 39 | result = data.frame(newVar = newVarName, 40 | Success = logical(length(newVarName)), 41 | Reason = character(length(newVarName)), 42 | stringsAsFactors = FALSE) 43 | printLab(paste("Constructing new variables (", n, " in Total)", sep = "")) 44 | for(i in 1:n){ 45 | cat(paste("(", i, "): ", sep = "")) 46 | if(origVar1[i] %in% colnames(data) && 47 | (origVar2[i] %in% colnames(data) || is.na(origVar2[i]))){ 48 | switch(constructType[i], 49 | share = {tmp = try(shConstruct(data = data, 50 | totVar = origVar2[i], 51 | shareVar = origVar1[i], 52 | newVarName = newVarName[i]), silent = TRUE)}, 53 | growth = {tmp = try(grConstruct(data = data, 54 | origVar = origVar1[i], 55 | newVarName = newVarName[i], 56 | type = grType[i], n = grFreq[i]), 57 | silent = TRUE)}, 58 | change = {tmp = try(chConstruct(data = data, 59 | origVar = origVar1[i], 60 | newVarName = newVarName[i], 61 | n = grFreq[i]), silent = TRUE)}, 62 | index = {tmp = try(indConstruct(data = data, 63 | origVar = origVar1[i], 64 | newVarName = newVarName[i], 65 | baseYear = baseYear), silent = TRUE)} 66 | ) 67 | ## Sometimes nan and Inf are a result of divisible by zero, in 68 | ## this case we will replace them with missing value for unknown 69 | ## information. Since Inf will usually cause problem in 70 | ## computation. 71 | print(str(tmp)) 72 | tmp[(is.nan(tmp[, 3]) | (tmp[, 3] == Inf)) & 73 | !is.na(tmp[, 3]), 3] = NA 74 | if(!(inherits(tmp, "try-error"))){ 75 | cat(paste("PASS: ", newVarName[i], 76 | " sucessfully constructed\n", sep = "")) 77 | result[i, "Success"] = TRUE 78 | result[i, "Reason"] = "Construction Successful" 79 | data = merge(data, tmp, by = c("FAOST_CODE", "Year")) 80 | } else { 81 | cat(paste("FAIL: ", newVarName[i], 82 | ", Check error message\n", sep = "")) 83 | result[i, "Success"] = FALSE 84 | result[i, "Reason"] = attr(tmp, "condition")$message 85 | } 86 | } else { 87 | cat(paste("FAIL: ", origVar1[i], " or ", 88 | origVar2[i], " not found in data\n", sep = "")) 89 | result[i, "Success"] = FALSE 90 | result[i, "Reason"] = "Variable not found in data" 91 | } 92 | } 93 | cat(paste("\nNumber of variables successfully computed: ", 94 | sum(result$Success), " out of ", NROW(result), "\n", sep = "")) 95 | list(data = data, result = result) 96 | } 97 | -------------------------------------------------------------------------------- /FAOSTAT/R/constructSYB.R: -------------------------------------------------------------------------------- 1 | ##' Construct/Creat new variable. 2 | ##' 3 | ##' A function used to construct new variables from existing variables. 4 | ##' 5 | ##' Currently two types of construction are supported, either share or 6 | ##' growth rate computation. 7 | ##' 8 | ##' Share can be a share of total or share of another variable depending 9 | ##' on whether an additional variable is supplied or not. 10 | ##' 11 | ##' @param data The data frame containing the raw variable 12 | ##' @param origVar1 The variable name to be used in construction, refer 13 | ##' to Details for more information and useage. 14 | ##' @param origVar2 The variable name to be used in construction, refer 15 | ##' to Details for more information and useage. 16 | ##' @param newVarName The name assigned to the new variable, if missing 17 | ##' then .SC/.SH/.GR/.CH will be appended depending on the type of 18 | ##' construction 19 | ##' @param constructType The type of construction, refer to Details 20 | ##' for more information. 21 | ##' @param grFreq The frequency for the growth rate to be computed. 22 | ##' @param grType The method for the growth to be calculated, currently 23 | ##' supports least squares and geometric. 24 | ##' @param baseYear The base year to be used for constructing index. 25 | ##' @export 26 | ##' @return A data frame containing both the original data frame and the 27 | ##' processed data and also a list indicating whether the construction 28 | ##' passed or failed. 29 | 30 | constructSYB = function(data, origVar1, origVar2, newVarName = NA, 31 | constructType = c("share", "growth", "change", "index"), 32 | grFreq = 1, grType = c("ls", "geo"), baseYear = 2000){ 33 | ## The length of the variables must be the same 34 | checkDim = try(data.frame(origVar1, origVar2, newVarName, 35 | constructType, grFreq, baseYear)) 36 | if(inherits(checkDim, "try-error")) 37 | stop("length of original variables are not the same") 38 | n = length(origVar1) 39 | result = data.frame(newVar = newVarName, 40 | Success = logical(length(newVarName)), 41 | Reason = character(length(newVarName)), 42 | stringsAsFactors = FALSE) 43 | printLab(paste("Constructing new variables (", n, " in Total)", sep = "")) 44 | for(i in 1:n){ 45 | cat(paste("(", i, "): ", sep = "")) 46 | if(origVar1[i] %in% colnames(data) && 47 | (origVar2[i] %in% colnames(data) || is.na(origVar2[i]))){ 48 | switch(constructType[i], 49 | share = {tmp = try(shConstruct(data = data, 50 | totVar = origVar2[i], 51 | shareVar = origVar1[i], 52 | newVarName = newVarName[i]), silent = TRUE)}, 53 | growth = {tmp = try(grConstruct(data = data, 54 | origVar = origVar1[i], 55 | newVarName = newVarName[i], 56 | type = grType[i], n = grFreq[i]), 57 | silent = TRUE)}, 58 | change = {tmp = try(chConstruct(data = data, 59 | origVar = origVar1[i], 60 | newVarName = newVarName[i], 61 | n = grFreq[i]), silent = TRUE)}, 62 | index = {tmp = try(indConstruct(data = data, 63 | origVar = origVar1[i], 64 | newVarName = newVarName[i], 65 | baseYear = baseYear), silent = TRUE)} 66 | ) 67 | ## Sometimes nan and Inf are a result of divisible by zero, in 68 | ## this case we will replace them with missing value for unknown 69 | ## information. Since Inf will usually cause problem in 70 | ## computation. 71 | print(str(tmp)) 72 | tmp[(is.nan(tmp[, 3]) | (tmp[, 3] == Inf)) & 73 | !is.na(tmp[, 3]), 3] = NA 74 | if(!(inherits(tmp, "try-error"))){ 75 | cat(paste("PASS: ", newVarName[i], 76 | " sucessfully constructed\n", sep = "")) 77 | result[i, "Success"] = TRUE 78 | result[i, "Reason"] = "Construction Successful" 79 | data = merge(data, tmp, by = c("FAOST_CODE", "Year")) 80 | } else { 81 | cat(paste("FAIL: ", newVarName[i], 82 | ", Check error message\n", sep = "")) 83 | result[i, "Success"] = FALSE 84 | result[i, "Reason"] = attr(tmp, "condition")$message 85 | } 86 | } else { 87 | cat(paste("FAIL: ", origVar1[i], " or ", 88 | origVar2[i], " not found in data\n", sep = "")) 89 | result[i, "Success"] = FALSE 90 | result[i, "Reason"] = "Variable not found in data" 91 | } 92 | } 93 | cat(paste("\nNumber of variables successfully computed: ", 94 | sum(result$Success), " out of ", NROW(result), "\n", sep = "")) 95 | list(data = data, result = result) 96 | } 97 | -------------------------------------------------------------------------------- /Codes/R/getFAO.R: -------------------------------------------------------------------------------- 1 | ##' Access to FAO FAOSTAT API. 2 | ##' 3 | ##' A function to access FAOSTAT data through the FAOSTAT API. 4 | ##' 5 | ##' Need to account for multiple itemCode, currently only support one 6 | ##' single variable. 7 | ##' 8 | ##' @param name The name to be given to the variable. 9 | ##' @param domainCode The domain of the data. 10 | ##' @param elementCode The code of the element. 11 | ##' @param itemCode The code of the specific item. 12 | ##' @param yearRange A numeric vector containing the years to be downloaded. 13 | ##' @param countrySet The FAOSTAT codes of those countries to be downloaded. 14 | ##' @param query The object created if using the FAOsearch function. 15 | ##' @param printURL Whether the url link for the data should be printed. 16 | ##' @param useCHMT logical, whether the CHMT function should be 17 | ##' applied to avoid double counting of China. 18 | ##' @param outputFormat The format of the data, can be 'long' or 'wide'. 19 | ##' @param returnNames Logical, should the area, the element and the 20 | ##' item names be reported?. 21 | ##' @param returnFlags, Logical, whether the flags should be 22 | ##' returned. Only work with outputFormat long. 23 | ##' @return Outputs a data frame containing the specified data. 24 | ##' @export 25 | ##' 26 | ##' 27 | ##' @seealso \code{\link{getWDI}}, \code{\link{getWDItoSYB}}, 28 | ##' \code{\link{getFAOtoSYB}}, \code{\link{FAOsearch}} 29 | ##' 30 | 31 | getFAO = function(name = NULL, domainCode = "RL", elementCode = 5110, 32 | itemCode = 6621, query, printURL = FALSE, 33 | useCHMT = TRUE, outputFormat = "wide", returnNames = FALSE, 34 | returnFlags = FALSE, yearRange = NULL, countrySet = NULL){ 35 | 36 | ## Year range 37 | if (!is.null(yearRange)) { 38 | if (!is.numeric(yearRange)) { 39 | stop("Please, provide a numeric vector for the year range.") 40 | } else { 41 | yearRange = paste(yearRange, collapse = ":") 42 | } 43 | } 44 | ## Country set 45 | if (!is.null(countrySet)) { 46 | if (!is.numeric(countrySet)) { 47 | stop("Please, provide a numeric vector for the year range.") 48 | } else { 49 | countrySet = paste(countrySet, collapse = ":") 50 | } 51 | } 52 | ## Query 53 | if(!missing(query)){ 54 | if(NROW(query) > 1) 55 | stop("Use 'getFAOtoSYB' for batch download") 56 | domainCode = query$domainCode 57 | itemCode = query$itemCode 58 | elementCode = query$elementCode 59 | if(is.null(query$name)){ 60 | name = with(query, paste(domainCode, itemCode, elementCode, sep = "_")) 61 | } else { 62 | name = query$name 63 | } 64 | } 65 | ## Name 66 | if(is.null(name)) 67 | name = paste(domainCode, itemCode, elementCode, sep = "_") 68 | 69 | base = c("http://faostat3.fao.org/wds-r/api?") 70 | database = "db=faostat4&" 71 | selection = "select=A.AreaCode[FAOST_CODE],D.year[Year],D.value[Value]" 72 | from = "&from=data[D],element[E],item[I],area[A]&" 73 | condition = paste0("where=D.elementcode(", elementCode, "),D.itemcode(", 74 | itemCode, "),D.domaincode('", domainCode, "')") 75 | if (!is.null(yearRange)) { 76 | condition = paste0(condition, ",D.year(", yearRange, ")") 77 | } 78 | if (!is.null(countrySet)) { 79 | condition = paste0(condition, ",A.AreaCode(", countrySet, ")") 80 | } 81 | join = ",JOIN(D.elementcode:E.elementcode),JOIN(D.itemcode:I.itemcode),JOIN(D.areacode:A.areacode)&orderby=E.elementnamee,D.year" 82 | 83 | ## Flags 84 | if(returnFlags){ 85 | outputFormat = "long" 86 | selection = paste0(selection, ",D.Flag[Flags]") 87 | } 88 | ## Names 89 | if(returnNames) 90 | selection = paste0(selection, "A.AreaNameE[AreaName],E.elementnamee[ElementName],I.itemnamee[ItemName]") 91 | ## Call 92 | out = "out=csv&" 93 | url = paste0(base, out, database, selection, from, condition, join) 94 | if(printURL) 95 | print(url) 96 | 97 | ## Allowing multiple server if any failed. 98 | for(i in 1:length(url)){ 99 | faoData = suppressWarnings(try(read.csv(file = url[i], 100 | stringsAsFactors = FALSE), silent = TRUE)) 101 | if(!inherits(faoData, "try-error")) 102 | break 103 | } 104 | faoData$FAOST_CODE = as.integer(faoData$FAOST_CODE) 105 | faoData$Year = as.integer(faoData$Year) 106 | ## CHMT 107 | if(useCHMT) 108 | faoData = CHMT(var = "Value", data = faoData, year = "Year") 109 | ## Output format 110 | if(outputFormat == "long" & NROW(faoData) != 0){ 111 | faoData$domainCode = domainCode 112 | faoData$itemCode = itemCode 113 | faoData$elementCode = elementCode 114 | faoData$name = name 115 | faoData$Value <- as.numeric(gsub("n.a.", "", faoData$Value)) 116 | } else if(outputFormat == "wide"){ 117 | colnames(faoData)[colnames(faoData) == "Value"] = name 118 | faoData[, name] <- as.numeric(gsub("n.a.", "", faoData[, name])) 119 | } 120 | faoData 121 | } 122 | -------------------------------------------------------------------------------- /FAOSTAT/R/getFAO.R: -------------------------------------------------------------------------------- 1 | ##' Access to FAO FAOSTAT API. 2 | ##' 3 | ##' A function to access FAOSTAT data through the FAOSTAT API. 4 | ##' 5 | ##' Need to account for multiple itemCode, currently only support one 6 | ##' single variable. 7 | ##' 8 | ##' @param name The name to be given to the variable. 9 | ##' @param domainCode The domain of the data. 10 | ##' @param elementCode The code of the element. 11 | ##' @param itemCode The code of the specific item. 12 | ##' @param yearRange A numeric vector containing the years to be downloaded. 13 | ##' @param countrySet The FAOSTAT codes of those countries to be downloaded. 14 | ##' @param query The object created if using the FAOsearch function. 15 | ##' @param printURL Whether the url link for the data should be printed. 16 | ##' @param useCHMT logical, whether the CHMT function should be 17 | ##' applied to avoid double counting of China. 18 | ##' @param outputFormat The format of the data, can be 'long' or 'wide'. 19 | ##' @param returnNames Logical, should the area, the element and the 20 | ##' item names be reported?. 21 | ##' @param returnFlags, Logical, whether the flags should be 22 | ##' returned. Only work with outputFormat long. 23 | ##' @return Outputs a data frame containing the specified data. 24 | ##' @export 25 | ##' 26 | ##' 27 | ##' @seealso \code{\link{getWDI}}, \code{\link{getWDItoSYB}}, 28 | ##' \code{\link{getFAOtoSYB}}, \code{\link{FAOsearch}} 29 | ##' 30 | 31 | getFAO = function(name = NULL, domainCode = "RL", elementCode = 5110, 32 | itemCode = 6621, query, printURL = FALSE, 33 | useCHMT = TRUE, outputFormat = "wide", returnNames = FALSE, 34 | returnFlags = FALSE, yearRange = NULL, countrySet = NULL){ 35 | 36 | ## Year range 37 | if (!is.null(yearRange)) { 38 | if (!is.numeric(yearRange)) { 39 | stop("Please, provide a numeric vector for the year range.") 40 | } else { 41 | yearRange = paste(yearRange, collapse = ":") 42 | } 43 | } 44 | ## Country set 45 | if (!is.null(countrySet)) { 46 | if (!is.numeric(countrySet)) { 47 | stop("Please, provide a numeric vector for countrySet.") 48 | } else { 49 | countrySet = paste(countrySet, collapse = ":") 50 | } 51 | } 52 | ## Query 53 | if(!missing(query)){ 54 | if(NROW(query) > 1) 55 | stop("Use 'getFAOtoSYB' for batch download") 56 | domainCode = query$domainCode 57 | itemCode = query$itemCode 58 | elementCode = query$elementCode 59 | if(is.null(query$name)){ 60 | name = with(query, paste(domainCode, itemCode, elementCode, sep = "_")) 61 | } else { 62 | name = query$name 63 | } 64 | } 65 | ## Name 66 | if(is.null(name)) 67 | name = paste(domainCode, itemCode, elementCode, sep = "_") 68 | 69 | base = c("http://faostat3.fao.org/wds-r/api?") 70 | database = "db=faostat4&" 71 | selection = "select=A.AreaCode[FAOST_CODE],D.year[Year],D.value[Value]" 72 | from = "&from=data[D],element[E],item[I],area[A]&" 73 | condition = paste0("where=D.elementcode(", elementCode, "),D.itemcode(", 74 | itemCode, "),D.domaincode('", domainCode, "')") 75 | if (!is.null(yearRange)) { 76 | condition = paste0(condition, ",D.year(", yearRange, ")") 77 | } 78 | if (!is.null(countrySet)) { 79 | condition = paste0(condition, ",A.AreaCode(", countrySet, ")") 80 | } 81 | join = ",JOIN(D.elementcode:E.elementcode),JOIN(D.itemcode:I.itemcode),JOIN(D.areacode:A.areacode)&orderby=E.elementnamee,D.year" 82 | 83 | ## Flags 84 | if(returnFlags){ 85 | outputFormat = "long" 86 | selection = paste0(selection, ",D.Flag[Flags]") 87 | } 88 | ## Names 89 | if(returnNames) 90 | selection = paste0(selection, "A.AreaNameE[AreaName],E.elementnamee[ElementName],I.itemnamee[ItemName]") 91 | ## Call 92 | out = "out=csv&" 93 | url = paste0(base, out, database, selection, from, condition, join) 94 | if(printURL) 95 | print(url) 96 | 97 | ## Allowing multiple server if any failed. 98 | for(i in 1:length(url)){ 99 | faoData = suppressWarnings(try(read.csv(file = url[i], 100 | stringsAsFactors = FALSE), silent = TRUE)) 101 | if(!inherits(faoData, "try-error")) 102 | break 103 | } 104 | faoData$FAOST_CODE = as.integer(faoData$FAOST_CODE) 105 | faoData$Year = as.integer(faoData$Year) 106 | ## CHMT 107 | if(useCHMT) 108 | faoData = CHMT(var = "Value", data = faoData, year = "Year") 109 | ## Output format 110 | if(outputFormat == "long" & NROW(faoData) != 0){ 111 | faoData$domainCode = domainCode 112 | faoData$itemCode = itemCode 113 | faoData$elementCode = elementCode 114 | faoData$name = name 115 | faoData$Value <- as.numeric(gsub("n.a.", "", faoData$Value)) 116 | } else if(outputFormat == "wide"){ 117 | colnames(faoData)[colnames(faoData) == "Value"] = name 118 | faoData[, name] <- as.numeric(gsub("n.a.", "", faoData[, name])) 119 | } 120 | faoData 121 | } 122 | -------------------------------------------------------------------------------- /Codes/R/getWDItoSYB.R: -------------------------------------------------------------------------------- 1 | ##' Access to World Bank WDI API 2 | ##' 3 | ##' The function downloads data from the World Bank API. 4 | ##' 5 | ##' 6 | ##' @param name The new name to be used in the column. 7 | ##' @param indicator The World Bank official indicator name. 8 | ##' @param startDate The start date for the data to begin 9 | ##' @param endDate The end date. 10 | ##' @param printURL Whether the url link for the data should be printed 11 | ##' @param getMetaData Whether the data definition and the meta data 12 | ##' should be downloaded as well. 13 | ##' @param printMetaData logical, print out the meta data information 14 | ##' @param saveMetaData logical, whether meta data should be saved as a 15 | ##' local csv file 16 | ##' @param outputFormat The format of the data, can be 'long' or 'wide'. 17 | ##' @return A list containing the following elements 18 | ##' \describe{ 19 | ##' \item{data}{The country level data} 20 | ##' \item{aggregates}{The aggregates provided by the World Bank} 21 | ##' \item{metaData}{The metaData associated with the data} 22 | ##' \item{results}{The status of the download, whether success/failed} 23 | ##' } 24 | ##' @export 25 | ##' 26 | ##' @seealso \code{\link{getWDI}}, \code{\link{getFAO}}, 27 | ##' \code{\link{getFAOtoSYB}} 28 | ##' @examples 29 | ##' ## pop.df = getWDItoSYB(name = "total_population", 30 | ##' ## indicator = "SP.POP.TOTL") 31 | ##' 32 | ##' 33 | 34 | getWDItoSYB = function(indicator = "SP.POP.0014.TO.ZS", name = NULL, 35 | startDate = 1960, endDate = format(Sys.Date(), "%Y"), printURL = FALSE, 36 | getMetaData = TRUE, printMetaData = FALSE, saveMetaData = FALSE, 37 | outputFormat = c("wide", "long")){ 38 | 39 | outputFormat = match.arg(outputFormat) 40 | 41 | n = length(indicator) 42 | if(is.null(name)) 43 | name = indicator 44 | 45 | if(length(name) != length(indicator)) 46 | stop("length of name need to be the same as indicator") 47 | downloadInfo = unique(data.frame(name = name, indicator = indicator, 48 | stringsAsFactors = FALSE)) 49 | wbData = data.frame(ISO2_WB_CODE = character(), Country = character(), 50 | Year = integer(), stringsAsFactors = FALSE) 51 | ## wbData = data.frame(ISO2_WB_CODE = "US", Country = "United States", 52 | ## Year = startDate, stringsAsFactors = FALSE) 53 | results = data.frame(Name = name, indicator = indicator, 54 | Success = logical(length(name)), 55 | Reason = character(length(name)), 56 | Time = as.POSIXct(rep(NA, length(name))), 57 | stringsAsFactors = FALSE) 58 | if(!(all(colSums(table(na.omit(downloadInfo))) == 1) & 59 | all(rowSums(table(na.omit(downloadInfo))) == 1))) 60 | stop("The relationship between the names and indicators are not 1-to-1") 61 | printLab(paste("World Bank Data Download (", n, " in Total)", sep = "")) 62 | i = 1 63 | retry = 1 64 | success = vector("logical", NROW(downloadInfo)) 65 | while(i <= n){ 66 | if(retry == 1) 67 | cat(paste("(", i, "): Downloading variable ", name[i], " ... ", 68 | sep = "")) 69 | if(is.na(indicator[i])){ 70 | cat("FAIL\n\tError: WDI indicator name missing\n") 71 | results[i, "Success"] = FALSE 72 | results[i, "Reason"] = "WDI indicator name missing" 73 | i = i + 1 74 | retry = 1 75 | } else { 76 | tmp = try(getWDI(indicator = downloadInfo[i, "indicator"], 77 | name = name[i], startDate = startDate, 78 | endDate = endDate, printURL = printURL, 79 | outputFormat = outputFormat)) 80 | if(!inherits(tmp, "try-error")){ 81 | ## colnames(tmp) = c("Country", "ISO2_WB_CODE", "Year", name[i]) 82 | if(outputFormat == "wide"){ 83 | wbData = merge(wbData, tmp, all = TRUE) 84 | } else if(outputFormat == "long"){ 85 | wbData = rbind(wbData, tmp) 86 | } 87 | results[i, "Success"] = TRUE 88 | results[i, "Reason"] = "Download Successful" 89 | results[i, "Time"] = Sys.time() 90 | i = i + 1 91 | retry = 1 92 | cat("OK\n") 93 | } else if(retry <= 10){ 94 | retry = retry + 1 95 | } else if(retry > 10){ 96 | cat("Download fail after 10 tries\n") 97 | results[i, "Success"] = FALSE 98 | results[i, "Reason"] = attr(tmp, "condition")$message 99 | i = i + 1 100 | retry = 1 101 | } 102 | } 103 | } 104 | 105 | if(getMetaData){ 106 | metaData = getWDImetaData(indicator = na.omit(indicator), 107 | printMetaData = printMetaData, 108 | saveMetaData = saveMetaData) 109 | } else { 110 | metaData = NULL 111 | } 112 | 113 | entity.df = arrange(with(wbData, wbData[ISO2_WB_CODE %in% 114 | FAOcountryProfile[, "ISO2_WB_CODE"], ]), ISO2_WB_CODE, Year) 115 | region.df = arrange(with(wbData, wbData[!(ISO2_WB_CODE %in% 116 | FAOcountryProfile[, "ISO2_WB_CODE"]), ]), ISO2_WB_CODE, Year) 117 | 118 | cat(paste("\nNumber of variables successfully downloaded: ", 119 | sum(results$Success), " out of ", NROW(results), "\n", sep = "")) 120 | list(entity = entity.df, aggregates = region.df, 121 | metaData = metaData, results = results) 122 | } 123 | 124 | utils::globalVariables(names = c("ISO2_WB_CODE", "Year")) 125 | -------------------------------------------------------------------------------- /FAOSTAT/R/getWDItoSYB.R: -------------------------------------------------------------------------------- 1 | ##' Access to World Bank WDI API 2 | ##' 3 | ##' The function downloads data from the World Bank API. 4 | ##' 5 | ##' 6 | ##' @param name The new name to be used in the column. 7 | ##' @param indicator The World Bank official indicator name. 8 | ##' @param startDate The start date for the data to begin 9 | ##' @param endDate The end date. 10 | ##' @param printURL Whether the url link for the data should be printed 11 | ##' @param getMetaData Whether the data definition and the meta data 12 | ##' should be downloaded as well. 13 | ##' @param printMetaData logical, print out the meta data information 14 | ##' @param saveMetaData logical, whether meta data should be saved as a 15 | ##' local csv file 16 | ##' @param outputFormat The format of the data, can be 'long' or 'wide'. 17 | ##' @return A list containing the following elements 18 | ##' \describe{ 19 | ##' \item{data}{The country level data} 20 | ##' \item{aggregates}{The aggregates provided by the World Bank} 21 | ##' \item{metaData}{The metaData associated with the data} 22 | ##' \item{results}{The status of the download, whether success/failed} 23 | ##' } 24 | ##' @export 25 | ##' 26 | ##' @seealso \code{\link{getWDI}}, \code{\link{getFAO}}, 27 | ##' \code{\link{getFAOtoSYB}} 28 | ##' @examples 29 | ##' ## pop.df = getWDItoSYB(name = "total_population", 30 | ##' ## indicator = "SP.POP.TOTL") 31 | ##' 32 | ##' 33 | 34 | getWDItoSYB = function(indicator = "SP.POP.0014.TO.ZS", name = NULL, 35 | startDate = 1960, endDate = format(Sys.Date(), "%Y"), printURL = FALSE, 36 | getMetaData = TRUE, printMetaData = FALSE, saveMetaData = FALSE, 37 | outputFormat = c("wide", "long")){ 38 | 39 | outputFormat = match.arg(outputFormat) 40 | 41 | n = length(indicator) 42 | if(is.null(name)) 43 | name = indicator 44 | 45 | if(length(name) != length(indicator)) 46 | stop("length of name need to be the same as indicator") 47 | downloadInfo = unique(data.frame(name = name, indicator = indicator, 48 | stringsAsFactors = FALSE)) 49 | wbData = data.frame(ISO2_WB_CODE = character(), Country = character(), 50 | Year = integer(), stringsAsFactors = FALSE) 51 | ## wbData = data.frame(ISO2_WB_CODE = "US", Country = "United States", 52 | ## Year = startDate, stringsAsFactors = FALSE) 53 | results = data.frame(Name = name, indicator = indicator, 54 | Success = logical(length(name)), 55 | Reason = character(length(name)), 56 | Time = as.POSIXct(rep(NA, length(name))), 57 | stringsAsFactors = FALSE) 58 | if(!(all(colSums(table(na.omit(downloadInfo))) == 1) & 59 | all(rowSums(table(na.omit(downloadInfo))) == 1))) 60 | stop("The relationship between the names and indicators are not 1-to-1") 61 | printLab(paste("World Bank Data Download (", n, " in Total)", sep = "")) 62 | i = 1 63 | retry = 1 64 | success = vector("logical", NROW(downloadInfo)) 65 | while(i <= n){ 66 | if(retry == 1) 67 | cat(paste("(", i, "): Downloading variable ", name[i], " ... ", 68 | sep = "")) 69 | if(is.na(indicator[i])){ 70 | cat("FAIL\n\tError: WDI indicator name missing\n") 71 | results[i, "Success"] = FALSE 72 | results[i, "Reason"] = "WDI indicator name missing" 73 | i = i + 1 74 | retry = 1 75 | } else { 76 | tmp = try(getWDI(indicator = downloadInfo[i, "indicator"], 77 | name = name[i], startDate = startDate, 78 | endDate = endDate, printURL = printURL, 79 | outputFormat = outputFormat)) 80 | if(!inherits(tmp, "try-error")){ 81 | ## colnames(tmp) = c("Country", "ISO2_WB_CODE", "Year", name[i]) 82 | if(outputFormat == "wide"){ 83 | wbData = merge(wbData, tmp, all = TRUE) 84 | } else if(outputFormat == "long"){ 85 | wbData = rbind(wbData, tmp) 86 | } 87 | results[i, "Success"] = TRUE 88 | results[i, "Reason"] = "Download Successful" 89 | results[i, "Time"] = Sys.time() 90 | i = i + 1 91 | retry = 1 92 | cat("OK\n") 93 | } else if(retry <= 10){ 94 | retry = retry + 1 95 | } else if(retry > 10){ 96 | cat("Download fail after 10 tries\n") 97 | results[i, "Success"] = FALSE 98 | results[i, "Reason"] = attr(tmp, "condition")$message 99 | i = i + 1 100 | retry = 1 101 | } 102 | } 103 | } 104 | 105 | if(getMetaData){ 106 | metaData = getWDImetaData(indicator = na.omit(indicator), 107 | printMetaData = printMetaData, 108 | saveMetaData = saveMetaData) 109 | } else { 110 | metaData = NULL 111 | } 112 | 113 | entity.df = arrange(with(wbData, wbData[ISO2_WB_CODE %in% 114 | FAOcountryProfile[, "ISO2_WB_CODE"], ]), ISO2_WB_CODE, Year) 115 | region.df = arrange(with(wbData, wbData[!(ISO2_WB_CODE %in% 116 | FAOcountryProfile[, "ISO2_WB_CODE"]), ]), ISO2_WB_CODE, Year) 117 | 118 | cat(paste("\nNumber of variables successfully downloaded: ", 119 | sum(results$Success), " out of ", NROW(results), "\n", sep = "")) 120 | list(entity = entity.df, aggregates = region.df, 121 | metaData = metaData, results = results) 122 | } 123 | 124 | utils::globalVariables(names = c("ISO2_WB_CODE", "Year")) 125 | -------------------------------------------------------------------------------- /FAOSTATdemo.R: -------------------------------------------------------------------------------- 1 | ########################################################################### 2 | ## Title: Demo of the FAOSTAT package 3 | ## Updated: 16/07/2014 4 | ## Notes: 5 | ########################################################################### 6 | 7 | # Install the package ----------------------------------------------------- 8 | 9 | if(!is.element("FAOSTAT", .packages(all.available = TRUE))) 10 | install_github(username = "mkao006", repo = "FAOSTATpackage", ref = "master", subdir = "FAOSTAT") 11 | library(FAOSTAT) 12 | help(package = "FAOSTAT") 13 | vignette("FAOSTAT", package = "FAOSTAT") 14 | 15 | # FAOsearch function ------------------------------------------------------ 16 | 17 | ## Use the interective function to search the codes. 18 | FAOsearch() 19 | ## Use the result of the search to download the data. 20 | test.df = getFAO(query = .LastSearch) 21 | 22 | # getFAO, getFAOtoSYB, and CHMT functions --------------------------------- 23 | 24 | ## A demonstration query 25 | FAOquery.df = data.frame(varName = c("arableLand", "cerealExp", "cerealProd"), 26 | domainCode = c("RL", "TP", "QC"), 27 | itemCode = c(6621, 1944, 1717), 28 | elementCode = c(5110, 5922, 5510), 29 | stringsAsFactors = FALSE) 30 | ## Download the data from FAOSTAT 31 | FAO.lst = with(FAOquery.df, 32 | getFAOtoSYB(name = varName, domainCode = domainCode, 33 | itemCode = itemCode, elementCode = elementCode, 34 | useCHMT = TRUE, outputFormat = "wide")) 35 | FAO.lst$entity[, "arableLand"] = as.numeric(FAO.lst$entity[, "arableLand"]) 36 | 37 | # FAOcheck function - multiChina ------------------------------------------ 38 | 39 | ## FAOcheck function 40 | FAOchecked.df = FAOcheck(var = FAOquery.df$varName, year = "Year", 41 | data = FAO.lst$entity, type = "multiChina", 42 | take = "simpleCheck") 43 | 44 | # getWDI and getWDItoSYB functions ---------------------------------------- 45 | 46 | ## Download World Bank data and meta-data 47 | WB.lst = getWDItoSYB(indicator = c("SP.POP.TOTL", "NY.GDP.MKTP.CD"), 48 | name = c("totalPopulation", "GDPUSD"), 49 | getMetaData = TRUE, printMetaData = TRUE) 50 | 51 | # fillCountryCode function ------------------------------------------------ 52 | 53 | ## Just a demonstration 54 | Demo = WB.lst$entity[, c("Country", "Year", "totalPopulation")] 55 | demoResult = fillCountryCode(country = "Country", data = Demo, 56 | outCode = "ISO2_WB_CODE") 57 | ## Countries have not been filled in 58 | unique(demoResult[is.na(demoResult$ISO2_WB_CODE), "Country"]) 59 | 60 | # mergeSYB and translateCountryCode functions ----------------------------- 61 | 62 | merged.df = mergeSYB(FAOchecked.df, WB.lst$entity, outCode = "FAOST_CODE") 63 | 64 | # translateUnit and scaleUnit functions ----------------------------------- 65 | 66 | multipliers = data.frame(Variable = c("arableLand", "cerealExp", "cerealProd", 67 | "totalPopulation", "GDPUSD"), 68 | Multipliers = c("thousand", NA, NA, NA, NA), 69 | stringsAsFactors = FALSE) 70 | multipliers[, "Multipliers"] = 71 | as.numeric(translateUnit(multipliers[, "Multipliers"])) 72 | preConstr.df = scaleUnit(merged.df, multipliers) 73 | 74 | # construct new variables ------------------------------------------------- 75 | 76 | con.df = data.frame(STS_ID = c("arableLandPC", "arableLandShareOfTotal", 77 | "totalPopulationGeoGR", "totalPopulationLsGR", 78 | "totalPopulationInd", "totalPopulationCh"), 79 | STS_ID_CONSTR1 = c(rep("arableLand", 2), 80 | rep("totalPopulation", 4)), 81 | STS_ID_CONSTR2 = c("totalPopulation", NA, NA, NA, NA, NA), 82 | STS_ID_WEIGHT = rep("totalPopulation", 6), 83 | CONSTRUCTION_TYPE = c("share", "share", "growth", "growth", 84 | "index", "change"), 85 | GROWTH_RATE_FREQ = c(NA, NA, 10, 10, NA, 1), 86 | GROWTH_TYPE = c(NA, NA, "geo", "ls", NA, NA), 87 | BASE_YEAR = c(NA, NA, NA, NA, 2000, NA), 88 | AGGREGATION = rep("weighted.mean", 6), 89 | THRESHOLD_PROP = rep(60, 6), 90 | stringsAsFactors = FALSE) 91 | 92 | postConstr.lst = with(con.df, 93 | constructSYB(data = preConstr.df, 94 | origVar1 = STS_ID_CONSTR1, 95 | origVar2 = STS_ID_CONSTR2, 96 | newVarName = STS_ID, 97 | constructType = CONSTRUCTION_TYPE, 98 | grFreq = GROWTH_RATE_FREQ, 99 | grType = GROWTH_TYPE, 100 | baseYear = BASE_YEAR)) 101 | 102 | 103 | 104 | # Aggregation ------------------------------------------------------------- 105 | 106 | ## Compute aggregates under the FAO continental region. 107 | relation.df = FAOregionProfile[, c("FAOST_CODE", "UNSD_MACRO_REG")] 108 | 109 | Macroregion.df = Aggregation(data = postConstr.lst$data, 110 | relationDF = relation.df, 111 | aggVar = c("arableLand", "totalPopulation", 112 | "arableLandPC"), 113 | weightVar = c(NA, NA, "totalPopulation"), 114 | aggMethod = c("sum", "sum", "weighted.mean"), 115 | applyRules = TRUE, 116 | keepUnspecified = TRUE, 117 | unspecifiedCode = "NotClassified", 118 | thresholdProp = c(rep(0.65,3))) 119 | -------------------------------------------------------------------------------- /FAOSTAT/demo/FAOSTATdemo.R: -------------------------------------------------------------------------------- 1 | ########################################################################### 2 | ## Title: Demo of the FAOSTAT package 3 | ## Updated: 16/07/2014 4 | ## Notes: 5 | ########################################################################### 6 | 7 | # Install the package ----------------------------------------------------- 8 | 9 | if(!is.element("FAOSTAT", .packages(all.available = TRUE))) 10 | install_github(username = "mkao006", repo = "FAOSTATpackage", ref = "master", subdir = "FAOSTAT") 11 | library(FAOSTAT) 12 | help(package = "FAOSTAT") 13 | vignette("FAOSTAT", package = "FAOSTAT") 14 | 15 | # FAOsearch function ------------------------------------------------------ 16 | 17 | ## Use the interective function to search the codes. 18 | FAOsearch() 19 | ## Use the result of the search to download the data. 20 | test.df = getFAO(query = .LastSearch) 21 | 22 | # getFAO, getFAOtoSYB, and CHMT functions --------------------------------- 23 | 24 | ## A demonstration query 25 | FAOquery.df = data.frame(varName = c("arableLand", "cerealExp", "cerealProd"), 26 | domainCode = c("RL", "TP", "QC"), 27 | itemCode = c(6621, 1944, 1717), 28 | elementCode = c(5110, 5922, 5510), 29 | stringsAsFactors = FALSE) 30 | ## Download the data from FAOSTAT 31 | FAO.lst = with(FAOquery.df, 32 | getFAOtoSYB(name = varName, domainCode = domainCode, 33 | itemCode = itemCode, elementCode = elementCode, 34 | useCHMT = TRUE, outputFormat = "wide")) 35 | FAO.lst$entity[, "arableLand"] = as.numeric(FAO.lst$entity[, "arableLand"]) 36 | 37 | # FAOcheck function - multiChina ------------------------------------------ 38 | 39 | ## FAOcheck function 40 | FAOchecked.df = FAOcheck(var = FAOquery.df$varName, year = "Year", 41 | data = FAO.lst$entity, type = "multiChina", 42 | take = "simpleCheck") 43 | 44 | # getWDI and getWDItoSYB functions ---------------------------------------- 45 | 46 | ## Download World Bank data and meta-data 47 | WB.lst = getWDItoSYB(indicator = c("SP.POP.TOTL", "NY.GDP.MKTP.CD"), 48 | name = c("totalPopulation", "GDPUSD"), 49 | getMetaData = TRUE, printMetaData = TRUE) 50 | 51 | # fillCountryCode function ------------------------------------------------ 52 | 53 | ## Just a demonstration 54 | Demo = WB.lst$entity[, c("Country", "Year", "totalPopulation")] 55 | demoResult = fillCountryCode(country = "Country", data = Demo, 56 | outCode = "ISO2_WB_CODE") 57 | ## Countries have not been filled in 58 | unique(demoResult[is.na(demoResult$ISO2_WB_CODE), "Country"]) 59 | 60 | # mergeSYB and translateCountryCode functions ----------------------------- 61 | 62 | merged.df = mergeSYB(FAOchecked.df, WB.lst$entity, outCode = "FAOST_CODE") 63 | 64 | # translateUnit and scaleUnit functions ----------------------------------- 65 | 66 | multipliers = data.frame(Variable = c("arableLand", "cerealExp", "cerealProd", 67 | "totalPopulation", "GDPUSD"), 68 | Multipliers = c("thousand", NA, NA, NA, NA), 69 | stringsAsFactors = FALSE) 70 | multipliers[, "Multipliers"] = 71 | as.numeric(translateUnit(multipliers[, "Multipliers"])) 72 | preConstr.df = scaleUnit(merged.df, multipliers) 73 | 74 | # construct new variables ------------------------------------------------- 75 | 76 | con.df = data.frame(STS_ID = c("arableLandPC", "arableLandShareOfTotal", 77 | "totalPopulationGeoGR", "totalPopulationLsGR", 78 | "totalPopulationInd", "totalPopulationCh"), 79 | STS_ID_CONSTR1 = c(rep("arableLand", 2), 80 | rep("totalPopulation", 4)), 81 | STS_ID_CONSTR2 = c("totalPopulation", NA, NA, NA, NA, NA), 82 | STS_ID_WEIGHT = rep("totalPopulation", 6), 83 | CONSTRUCTION_TYPE = c("share", "share", "growth", "growth", 84 | "index", "change"), 85 | GROWTH_RATE_FREQ = c(NA, NA, 10, 10, NA, 1), 86 | GROWTH_TYPE = c(NA, NA, "geo", "ls", NA, NA), 87 | BASE_YEAR = c(NA, NA, NA, NA, 2000, NA), 88 | AGGREGATION = rep("weighted.mean", 6), 89 | THRESHOLD_PROP = rep(60, 6), 90 | stringsAsFactors = FALSE) 91 | 92 | postConstr.lst = with(con.df, 93 | constructSYB(data = preConstr.df, 94 | origVar1 = STS_ID_CONSTR1, 95 | origVar2 = STS_ID_CONSTR2, 96 | newVarName = STS_ID, 97 | constructType = CONSTRUCTION_TYPE, 98 | grFreq = GROWTH_RATE_FREQ, 99 | grType = GROWTH_TYPE, 100 | baseYear = BASE_YEAR)) 101 | 102 | 103 | 104 | # Aggregation ------------------------------------------------------------- 105 | 106 | ## Compute aggregates under the FAO continental region. 107 | relation.df = FAOregionProfile[, c("FAOST_CODE", "UNSD_MACRO_REG")] 108 | 109 | Macroregion.df = Aggregation(data = postConstr.lst$data, 110 | relationDF = relation.df, 111 | aggVar = c("arableLand", "totalPopulation", 112 | "arableLandPC"), 113 | weightVar = c(NA, NA, "totalPopulation"), 114 | aggMethod = c("sum", "sum", "weighted.mean"), 115 | applyRules = TRUE, 116 | keepUnspecified = TRUE, 117 | unspecifiedCode = "NotClassified", 118 | thresholdProp = c(rep(0.65,3))) 119 | -------------------------------------------------------------------------------- /Codes/R/getFAOtoSYB.R: -------------------------------------------------------------------------------- 1 | ##' Access to FAO FAOSTAT API 2 | ##' 3 | ##' A wrapper function using getFAO() to obtain and process multiple 4 | ##' data set to obtain data. 5 | ##' 6 | ##' @param name The name to be given to the variable. 7 | ##' @param domainCode The domain code of the variable, see details. 8 | ##' @param elementCode The element code of the variable, see details. 9 | ##' @param itemCode The item code of the variable, see details. 10 | ##' @param query The object created if using the FAOsearch function 11 | ##' @param printURL Whether the url link for the data should be printed 12 | ##' @param useCHMT logical, whether the CHMT function should be 13 | ##' @param outputFormat The format of the data, can be 'long' or 'wide'. 14 | ##' appied to avoid double counting of China. 15 | ##' @param returnFlags, Logical, whether the flags should be 16 | ##' returned. Only work with outputFormat long. 17 | ##' @param yearRange A numeric vector containing the years to be downloaded. 18 | ##' @param countrySet The FAOSTAT codes of those countries to be downloaded. 19 | ##' 20 | ##' @return A list containing the following elements 21 | ##' \describe{ 22 | ##' \item{entity}{The entity level data} 23 | ##' \item{aggregates}{The aggregates provided by the FAO} 24 | ##' \item{results}{The status of the download, whether success/failed} 25 | ##' } 26 | ##' @export 27 | ##' @seealso \code{\link{getWDI}}, \code{\link{getFAO}}, 28 | ##' \code{\link{getWDItoSYB}} 29 | ##' 30 | ##' @examples 31 | ##' ## The default option is the arable land area 32 | ##' ## arlLand.lst = getFAOtoSYB() 33 | 34 | getFAOtoSYB = function(name = NULL, domainCode = "RL", 35 | elementCode = 5110, itemCode = 6621, query, printURL = FALSE, 36 | useCHMT = TRUE, yearRange = NULL, countrySet = NULL, 37 | outputFormat = c("wide", "long"), returnFlags = FALSE){ 38 | outputFormat = match.arg(outputFormat) 39 | if(returnFlags) 40 | outputFormat = "long" 41 | 42 | if(!missing(query)){ 43 | domainCode = query$domainCode 44 | itemCode = query$itemCode 45 | elementCode = query$elementCode 46 | if(is.null(query$name)){ 47 | name = with(query, paste(domainCode, itemCode, elementCode, sep = "_")) 48 | } else { 49 | name = query$name 50 | } 51 | } 52 | 53 | if(is.null(name)) 54 | name = paste(domainCode, itemCode, elementCode, sep = "_") 55 | n = length(name) 56 | if(any(length(domainCode) != n, length(elementCode) != n, 57 | length(itemCode) != n)) 58 | stop("length of inputs are not all the same, check the number of names") 59 | 60 | faoData = data.frame(FAOST_CODE = integer(), 61 | Year = integer(), stringsAsFactors = FALSE) 62 | results = data.frame(Name = name, Success = logical(length(name)), 63 | Reason = character(length(name)), 64 | Time = as.POSIXct(rep(NA, length(name))), 65 | stringsAsFactors = FALSE) 66 | printLab(paste("FAOSTAT Data Download (", n, " in Total)", sep = "")) 67 | 68 | i = 1 69 | retry = 1 70 | while(i <= n){ 71 | if(retry == 1) 72 | cat(paste("(", i, "): Downloading variable ", name[i], " ... ", 73 | sep = "")) 74 | if(any(is.na(domainCode[i]), is.na(elementCode[i]), is.na(itemCode)[i])){ 75 | cat("FAIL\n\t Error: One of Domain, Element or Item code is missing\n") 76 | results[i, "Success"] = FALSE 77 | results[i, "Reason"] = "One of Domain, Element or Item code is missing" 78 | } else { 79 | tmp = try(getFAO(name = name[i], 80 | domainCode = domainCode[i], 81 | elementCode = elementCode[i], 82 | itemCode = itemCode[i], printURL = printURL, 83 | useCHMT = useCHMT, outputFormat = outputFormat, 84 | returnFlags = returnFlags, 85 | yearRange = yearRange, 86 | countrySet = countrySet)) 87 | if(!inherits(tmp, "try-error")){ 88 | ## This was to account sometimes the download is successful, yet 89 | ## the data frame is empty 90 | if(NROW(tmp) != 0){ 91 | cat("OK\n") 92 | results[i, "Success"] = TRUE 93 | results[i, "Reason"] = "Download Successful" 94 | results[i, "Time"] = Sys.time() 95 | if(outputFormat == "wide"){ 96 | faoData = merge(x = faoData, y = tmp, all = TRUE, 97 | by = c("FAOST_CODE", "Year")) 98 | } else if(outputFormat == "long"){ 99 | faoData = rbind(faoData, tmp) 100 | } 101 | i = i + 1 102 | retry = 1 103 | } else { 104 | tmp = c("The specified query has no data, consult FAOSTAT") 105 | cat(paste(tmp, "\n")) 106 | class(tmp) = "try-error" 107 | attr(tmp, "condition") = 108 | list(message = tmp, call = NULL) 109 | i = i + 1 110 | retry = 1 111 | } 112 | } else { 113 | if(retry <=50){ 114 | print(retry) 115 | retry = retry + 1 116 | } else { 117 | cat("Download fail after 50 tries\n") 118 | results[i, "Success"] = FALSE 119 | results[i, "Reason"] = attr(tmp, "condition")$message 120 | i = i + 1 121 | retry = 1 122 | } 123 | } 124 | } 125 | } 126 | entity.df = arrange(with(faoData, faoData[FAOST_CODE %in% 127 | FAOcountryProfile[, "FAOST_CODE"], ]), FAOST_CODE, Year) 128 | region.df = arrange(with(faoData, faoData[!(FAOST_CODE %in% 129 | FAOcountryProfile[, "FAOST_CODE"]), ]), FAOST_CODE, Year) 130 | cat(paste("\n Number of variables successfully downloaded: ", 131 | sum(results$Success), " out of ", NROW(results), "\n\n", sep = "")) 132 | list(entity = entity.df, aggregates = region.df, results = results) 133 | } 134 | 135 | ## The following two variables are hard coded 136 | utils::globalVariables(names = c("FAOST_CODE", "Year")) 137 | -------------------------------------------------------------------------------- /FAOSTAT/R/getFAOtoSYB.R: -------------------------------------------------------------------------------- 1 | ##' Access to FAO FAOSTAT API 2 | ##' 3 | ##' A wrapper function using getFAO() to obtain and process multiple 4 | ##' data set to obtain data. 5 | ##' 6 | ##' @param name The name to be given to the variable. 7 | ##' @param domainCode The domain code of the variable, see details. 8 | ##' @param elementCode The element code of the variable, see details. 9 | ##' @param itemCode The item code of the variable, see details. 10 | ##' @param query The object created if using the FAOsearch function 11 | ##' @param printURL Whether the url link for the data should be printed 12 | ##' @param useCHMT logical, whether the CHMT function should be 13 | ##' @param outputFormat The format of the data, can be 'long' or 'wide'. 14 | ##' appied to avoid double counting of China. 15 | ##' @param returnFlags, Logical, whether the flags should be 16 | ##' returned. Only work with outputFormat long. 17 | ##' @param yearRange A numeric vector containing the years to be downloaded. 18 | ##' @param countrySet The FAOSTAT codes of those countries to be downloaded. 19 | ##' 20 | ##' @return A list containing the following elements 21 | ##' \describe{ 22 | ##' \item{entity}{The entity level data} 23 | ##' \item{aggregates}{The aggregates provided by the FAO} 24 | ##' \item{results}{The status of the download, whether success/failed} 25 | ##' } 26 | ##' @export 27 | ##' @seealso \code{\link{getWDI}}, \code{\link{getFAO}}, 28 | ##' \code{\link{getWDItoSYB}} 29 | ##' 30 | ##' @examples 31 | ##' ## The default option is the arable land area 32 | ##' ## arlLand.lst = getFAOtoSYB() 33 | 34 | getFAOtoSYB = function(name = NULL, domainCode = "RL", 35 | elementCode = 5110, itemCode = 6621, query, printURL = FALSE, 36 | useCHMT = TRUE, yearRange = NULL, countrySet = NULL, 37 | outputFormat = c("wide", "long"), returnFlags = FALSE){ 38 | outputFormat = match.arg(outputFormat) 39 | if(returnFlags) 40 | outputFormat = "long" 41 | 42 | if(!missing(query)){ 43 | domainCode = query$domainCode 44 | itemCode = query$itemCode 45 | elementCode = query$elementCode 46 | if(is.null(query$name)){ 47 | name = with(query, paste(domainCode, itemCode, elementCode, sep = "_")) 48 | } else { 49 | name = query$name 50 | } 51 | } 52 | 53 | if(is.null(name)) 54 | name = paste(domainCode, itemCode, elementCode, sep = "_") 55 | n = length(name) 56 | if(any(length(domainCode) != n, length(elementCode) != n, 57 | length(itemCode) != n)) 58 | stop("length of inputs are not all the same, check the number of names") 59 | 60 | faoData = data.frame(FAOST_CODE = integer(), 61 | Year = integer(), stringsAsFactors = FALSE) 62 | results = data.frame(Name = name, Success = logical(length(name)), 63 | Reason = character(length(name)), 64 | Time = as.POSIXct(rep(NA, length(name))), 65 | stringsAsFactors = FALSE) 66 | printLab(paste("FAOSTAT Data Download (", n, " in Total)", sep = "")) 67 | 68 | i = 1 69 | retry = 1 70 | while(i <= n){ 71 | if(retry == 1) 72 | cat(paste("(", i, "): Downloading variable ", name[i], " ... ", 73 | sep = "")) 74 | if(any(is.na(domainCode[i]), is.na(elementCode[i]), is.na(itemCode)[i])){ 75 | cat("FAIL\n\t Error: One of Domain, Element or Item code is missing\n") 76 | results[i, "Success"] = FALSE 77 | results[i, "Reason"] = "One of Domain, Element or Item code is missing" 78 | } else { 79 | tmp = try(getFAO(name = name[i], 80 | domainCode = domainCode[i], 81 | elementCode = elementCode[i], 82 | itemCode = itemCode[i], printURL = printURL, 83 | useCHMT = useCHMT, outputFormat = outputFormat, 84 | returnFlags = returnFlags, 85 | yearRange = yearRange, 86 | countrySet = countrySet)) 87 | if(!inherits(tmp, "try-error")){ 88 | ## This was to account sometimes the download is successful, yet 89 | ## the data frame is empty 90 | if(NROW(tmp) != 0){ 91 | cat("OK\n") 92 | results[i, "Success"] = TRUE 93 | results[i, "Reason"] = "Download Successful" 94 | results[i, "Time"] = Sys.time() 95 | if(outputFormat == "wide"){ 96 | faoData = merge(x = faoData, y = tmp, all = TRUE, 97 | by = c("FAOST_CODE", "Year")) 98 | } else if(outputFormat == "long"){ 99 | faoData = rbind(faoData, tmp) 100 | } 101 | i = i + 1 102 | retry = 1 103 | } else { 104 | tmp = c("The specified query has no data, consult FAOSTAT") 105 | cat(paste(tmp, "\n")) 106 | class(tmp) = "try-error" 107 | attr(tmp, "condition") = 108 | list(message = tmp, call = NULL) 109 | i = i + 1 110 | retry = 1 111 | } 112 | } else { 113 | if(retry <=50){ 114 | print(retry) 115 | retry = retry + 1 116 | } else { 117 | cat("Download fail after 50 tries\n") 118 | results[i, "Success"] = FALSE 119 | results[i, "Reason"] = attr(tmp, "condition")$message 120 | i = i + 1 121 | retry = 1 122 | } 123 | } 124 | } 125 | } 126 | entity.df = arrange(with(faoData, faoData[FAOST_CODE %in% 127 | FAOcountryProfile[, "FAOST_CODE"], ]), FAOST_CODE, Year) 128 | region.df = arrange(with(faoData, faoData[!(FAOST_CODE %in% 129 | FAOcountryProfile[, "FAOST_CODE"]), ]), FAOST_CODE, Year) 130 | cat(paste("\n Number of variables successfully downloaded: ", 131 | sum(results$Success), " out of ", NROW(results), "\n\n", sep = "")) 132 | list(entity = entity.df, aggregates = region.df, results = results) 133 | } 134 | 135 | ## The following two variables are hard coded 136 | utils::globalVariables(names = c("FAOST_CODE", "Year")) 137 | -------------------------------------------------------------------------------- /FAOSTATbuild.R: -------------------------------------------------------------------------------- 1 | ########################################################################### 2 | ## Title: This script builds the FAOSTAT package 3 | ## Created: 09/05/2013 4 | ## Updated: 09/07/2014 5 | ########################################################################### 6 | 7 | # Needed libraries -------------------------------------------------------- 8 | 9 | library(roxygen2) 10 | library(RJSONIO) 11 | library(utils) 12 | library(knitr) 13 | 14 | # Ghost script ------------------------------------------------------------ 15 | 16 | Sys.setenv(R_GSCMD='"C:/Program Files/gs/gs9.07/bin/gswin32c.exe"') 17 | 18 | # FAOcountryProfile ------------------------------------------------------- 19 | 20 | tmp <- 21 | read.csv(file = "FAOcountryProfileUTF8.csv", header = TRUE, 22 | stringsAsFactors = FALSE, na.string = "", encoding = "UTF-8") 23 | tmp[tmp == " "] <- NA 24 | FAOcountryProfile <- 25 | tmp[, c("FAOST_CODE", "ADM0_CODE", "ISO2_CODE", "ISO2_WB_CODE", 26 | "ISO3_CODE", "ISO3_WB_CODE", "UN_CODE", "CRS_CODE", "MOTHER_M49_CODE", 27 | "M49_FAOST_CODE", "FAO_TABLE_NAME", "OFFICIAL_FAO_NAME", 28 | "UNOFFICIAL1_NAME", "UNOFFICIAL2_NAME", "UNOFFICIAL3_NAME", 29 | "SHORT_NAME", "ABBR_FAO_NAME_SP", "UN_YEAR_START_INFO", 30 | "UN_YEAR_END_INFO", "FORMER_MEMBER_INFO", "COMPOSITION_INFO", 31 | "UN_STATUS_INFO")] 32 | save(FAOcountryProfile, file = "FAOcountryProfile.RData") 33 | 34 | FAOregionProfile <- tmp 35 | save(FAOregionProfile, file = "FAOregionProfile.RData") 36 | 37 | # FAOmetaTable ------------------------------------------------------------ 38 | 39 | ## Groups 40 | # urlGrp <- "http://fenix.fao.org/wds/rest/groups/faostat2/en" 41 | # urlGrp <- "http://fenixapps2.fao.org/wds/rest/groups/faostat2/en" 42 | urlGrp <- "http://faostat3.fao.org/wds/rest/groups/faostat2/en" 43 | groupCode <- 44 | unique(data.frame(groupCode = sapply(fromJSON(urlGrp, encoding = "UTF-8"), 45 | function(x) x[1]), 46 | groupName = sapply(fromJSON(urlGrp, encoding = "UTF-8"), 47 | function(x) x[2]), 48 | stringsAsFactors = FALSE)) 49 | 50 | ## Domains 51 | # urlDom <- "http://fenix.fao.org/wds/rest/domains/faostat2/" 52 | # urlDom <- "http://fenixapps2.fao.org/wds/rest/domains/faostat2/" 53 | urlDom <- "http://faostat3.fao.org/wds/rest/domains/faostat2/" 54 | base <- data.frame() 55 | for(i in 1:NROW(groupCode)){ 56 | tmp <- fromJSON(paste(urlDom, groupCode[i, "groupCode"], "/en", sep = ""), 57 | encoding = "UTF-8") 58 | tmp2 <- unique(data.frame(groupCode = groupCode[i, "groupCode"], 59 | domainCode = sapply(tmp, function(x) x[1]), 60 | domainName = sapply(tmp, function(x) x[2]), 61 | stringsAsFactors = FALSE)) 62 | base <- rbind(base, tmp2) 63 | } 64 | domainCode <- base 65 | 66 | ## Elements 67 | # base <- data.frame() 68 | # for(i in 1:NROW(domainCode)){ 69 | # tmp <- try(fromJSON(paste("http://faostat3.fao.org/wds/rest/procedures/elements/faostat2/", 70 | # domainCode[i, "domainCode"], "/en", sep = ""), encoding = "UTF-8")) 71 | # if(!inherits(tmp, "try-error") & length(tmp) != 0){ 72 | # tmp2 <- unique(data.frame(domainCode = domainCode[i, "domainCode"], 73 | # elementCode = sapply(tmp, function(x) x[1]), 74 | # elementName = sapply(tmp, function(x) 75 | # paste0(x[2], "(", x[3], ")")), 76 | # stringsAsFactors = FALSE)) 77 | # base <- rbind(base, tmp2) 78 | # } 79 | # } 80 | # elemCode <- base 81 | 82 | elemCode <- read.csv("./DomainItemElement.csv", header = TRUE) 83 | elemCode <- unique(elemCode[,c("DomainCode", "ElementCode")]) 84 | elementDescription <- read.csv("./ElementDescription.csv", header = TRUE) 85 | elementDescription <- elementDescription[, c("ElementCode", "ElementUnitNameE")] 86 | elemCode <- merge(elemCode, elementDescription, by = "ElementCode", all.x = TRUE) 87 | colnames(elemCode) <- c("elementCode", "domainCode", "elementName") 88 | elemCode <- elemCode[, c("domainCode", "elementCode", "elementName")] 89 | 90 | ## Items 91 | base <- data.frame() 92 | for(i in 1:NROW(domainCode)){ 93 | tmp <- try(fromJSON(paste("http://faostat3.fao.org/wds/rest/procedures/items/faostat2/", 94 | domainCode[i, "domainCode"], "/en", sep = ""), encoding = "UTF-8")) 95 | if(!inherits(tmp, "try-error") & length(tmp) != 0){ 96 | tmp2 <- unique(data.frame(domainCode = domainCode[i, "domainCode"], 97 | itemCode = sapply(tmp, function(x) x[1]), 98 | itemName = sapply(tmp, function(x) x[2]), 99 | stringsAsFactors = FALSE)) 100 | base <- rbind(base, tmp2) 101 | } 102 | } 103 | itemCode <- base 104 | 105 | ## Items aggregated 106 | base <- data.frame() 107 | for(i in 1:NROW(domainCode)){ 108 | tmp <- try(fromJSON(paste("http://faostat3.fao.org/wds/rest/procedures/itemsaggregated/faostat2/", 109 | domainCode[i, "domainCode"], "/en", sep = ""), encoding = "UTF-8")) 110 | if(!inherits(tmp, "try-error") & length(tmp) != 0){ 111 | tmp2 <- unique(data.frame(domainCode = domainCode[i, "domainCode"], 112 | itemCode = sapply(tmp, function(x) x[1]), 113 | itemName = sapply(tmp, function(x) x[2]), 114 | stringsAsFactors = FALSE)) 115 | base <- rbind(base, tmp2) 116 | } 117 | } 118 | itemAggCode <- base 119 | 120 | ## Table 121 | FAOmetaTable <- 122 | list(groupTable = groupCode, domainTable = domainCode, 123 | itemTable = itemCode, itemAggTable = itemAggCode, elementTable = elemCode) 124 | save(FAOmetaTable, file = "FAOmetaTable.RData") 125 | 126 | # Building the package ---------------------------------------------------- 127 | 128 | ## Remove the folder if it exists 129 | if(file.exists("./FAOSTAT")) 130 | unlink("FAOSTAT", recursive = TRUE) 131 | 132 | ## Build the package 133 | package.skeleton("FAOSTAT", code_files = paste("./Codes/R/", 134 | dir("./Codes/R/", 135 | pattern = "\\.R$"), sep = ""), 136 | force = FALSE) 137 | 138 | ## Include the data 139 | dir.create("FAOSTAT/data") 140 | file.copy(from = "./FAOcountryProfile.RData", 141 | to = "FAOSTAT/data/", overwrite = TRUE) 142 | file.copy(from = "./FAOregionProfile.RData", 143 | to = "FAOSTAT/data/", overwrite = TRUE) 144 | file.copy(from = "./FAOmetaTable.RData", 145 | to = "FAOSTAT/data/", overwrite = TRUE) 146 | file.copy(from = "./DESCRIPTION", to = "FAOSTAT/", 147 | overwrite = TRUE) 148 | unlink("./FAOSTAT/Read\\-and\\-delete\\-me") 149 | 150 | ## Include Demo 151 | dir.create("FAOSTAT/demo") 152 | file.copy(from = "./FAOSTATdemo.R", 153 | to = "FAOSTAT/demo/", overwrite = TRUE) 154 | cat("FAOSTATdemo Demonstration for the FAOSTAT package\n", 155 | file = "FAOSTAT/demo/00Index") 156 | 157 | ## Include tests 158 | dir.create("FAOSTAT/tests") 159 | file.copy("Codes/tests", "FAOSTAT", recursive = TRUE) 160 | 161 | ## Use roxygen to build the documentation 162 | roxygenize("FAOSTAT") 163 | 164 | ## Include vignette 165 | dir.create("./FAOSTAT/vignettes/") 166 | dir.create("./FAOSTAT/inst/") 167 | dir.create("./FAOSTAT/inst/doc/") 168 | file.copy(from = "./Documentation/FAOSTAT.pdf", 169 | to = "./FAOSTAT/inst/doc/", overwrite = TRUE) 170 | file.copy(from = "./Documentation/FAOSTAT.Rnw", 171 | to = "./FAOSTAT/vignettes/", overwrite = TRUE) 172 | file.copy(from = "./Documentation/FAOSTAT.pdf", 173 | to = "./FAOSTAT/vignettes/", overwrite = TRUE) 174 | 175 | ## Build and check the package 176 | system("R CMD INSTALL --build FAOSTAT") 177 | system("R CMD build FAOSTAT") 178 | system("R CMD check --as-cran --timings FAOSTAT") 179 | 180 | ########################################################################### 181 | ## End 182 | ########################################################################### 183 | --------------------------------------------------------------------------------