-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitial notebook.Rmd
More file actions
77 lines (59 loc) · 2.03 KB
/
Initial notebook.Rmd
File metadata and controls
77 lines (59 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: "Initial Notebook"
output: html_notebook
---
Bash run:
sudo apt-get install libcurl4-openssl-dev libssl-dev libxml2-dev
Install dependencies
```{r}
install.packages("devtools")
install.packages(c('RJSONIO','plyr','data.table','classInt','ggplot2','labeling', 'readr'))
```
Specific datasets of interest:
BC=3=crop_stock
CC=5=crop_supply
OA=48=crop_production
QC=57=population
TP=74=crop_trade
```{r functions, output=F}
devtools::load_all("FAOSTATpackage/FAOSTAT")
fao <- FAOsearch(full=T)
# Retrieved: https://github.com/muuankarski/faobulk/blob/master/R/get_data.R
get_data <- function(DatasetCode = "QP"){
datas <- fao
urli <- datas[datas$DatasetCode == DatasetCode,]$FileLocation
fly <- tempfile(fileext = ".zip")
download.file(url = urli, destfile = fly)
dat <- readr::read_csv(fly)
names(dat) <- tolower(sub(" ", "_", names(dat)))
return(dat)
}
```
```{r}
# Download complete FAO zip: fenixservices.fao.org/faostat/static/bulkdownloads/FAOSTAT.zip
write.to.table <- function(table_acronym){
table <- get_data(table_acronym)[,c(2,4,6,8,9,10)]
write.table(table, "crop_data.csv", append = T, sep = "|")
}
write.to.table("OA")
# Specific tables
population <- get_data("OA")[,c(2,4,6,8,9,10)]
write.table(population, "crop_total.csv", sep = "|")
rm(population)
crop_stock <- get_data("BC")[,c(2,4,6,8,9,10)]
write.table(crop_stock, "crop_total.csv", append = T, sep = "|")
rm(crop_stock)
crop_supply <- get_data("CC")[,c(2,4,6,8,9,10)]
write.table(crop_supply, "crop_total.csv", append = T, sep = "|")
rm(crop_supply)
crop_production <- get_data("QC")[,c(2,4,6,8,9,10)]
write.table(crop_production, "crop_total.csv", append = T, sep = "|")
rm(crop_production)
crop_trade <- get_data("TP")[,c(2,4,6,8,9,10)]
write.table(crop_trade, "crop_total.csv", append = T, sep = "|")
rm(crop_trade)
# crop_total <- rbind(crop_production, crop_stock, crop_supply, crop_trade)
# rm(crop_production, crop_stock, crop_supply, crop_trade)
# crop_total <- crop_total[!crop_total$value==0,]
# crop_total <- crop_total[complete.cases(crop_total),]
```