You need to sign in or sign up before continuing.
Newer
Older
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#+eval=FALSE
#...................................................................................#
#
# USE CASE 3 example
#
# Perform analysis
#
#...................................................................................#
# Always load fadnUtils and data.table
library(fadnUtils)
library(data.table)
# The fist step is to set the current data.dir
set.data.dir("H:/IFM-CAP/sample.fadnutils.dir")
# Let's see what countries and years are available for loading
show.data.dir.contents()
#We load structured data for all available countries and years
my.data = load.fadn.str.rds()
# .............. HOW MANY FARMS FOR EACH COUNTY AND EACH YEAR .....................#
# we use the info DT, and group by YEAR-COUNTRY
my.data$info[,.N,by=list(YEAR,COUNTRY)]
#We can also use dcast, to show a more tabular format
dcast(
my.data$info,
YEAR~COUNTRY,
fun.aggregate = length,
value.var =
)
# We can also export to clipboard, using the write.excel utility function
# After running the following command, open excel and paste. The result will appear.
write.excel(
dcast(
my.data$info,
YEAR~COUNTRY,
fun.aggregate = length,
value.var =
)
)
# .............. ALL CROP AREAS PER COUNTRY-YEAR ...................................#
# First, calculate the weighted area
my.data$crops[
VARIABLE=="LEVL",
VALUE.w:=WEIGHT*VALUE/1000
]
# Then dcast that variable
dcast(
my.data$crops[VARIABLE=="LEVL"],
COUNTRY+CROP~YEAR,
value.var = "VALUE.w",
fun.aggregate = sum,
na.rm = T
)
# .............. ALL CROP PRODUCTION PER COUNTRY-YEAR..............................#
dcast(
my.data$crops[VARIABLE=="GROF",VALUE.w:=WEIGHT*VALUE/1000],
COUNTRY+CROP~YEAR,
value.var = "VALUE.w",
fun.aggregate = sum,
na.rm = T
)
# .............. BARLEY PRODUCTION PER COUNTRY-YEAR................................#
dcast(
my.data$crops[
VARIABLE=="GROF" & CROP=="BARL",
VALUE.w:=WEIGHT*VALUE/1000
],
COUNTRY~YEAR,
value.var = "VALUE.w",
fun.aggregate = sum,
na.rm = T
)
# .............. DISTRIBUTION OF NUMBER OF CROPS PER COUNTRY-YEAR ..................#
crops.data = my.data$crops #catering for easier access at next steps
#this contains the number of crops for each farm-country-year/
# Be carefule, we hav to filter to count only the LEVL variable
crops.data.Ncrops = crops.data[VARIABLE=="LEVL",.N,by=list(COUNTRY,YEAR,ID)]
# This displays the quantiles of the number of crops
crops.data.Ncrops[,as.list(quantile(N)),by=list(YEAR,COUNTRY)][order(COUNTRY)]
# R excels on graphic representation of results
library(ggplot2)
ggplot(crops.data.Ncrops,aes(y=N,x=1)) +
geom_boxplot() +
facet_grid(YEAR~COUNTRY) +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()
)+
ylab("Number of Crops")