forked from RLadiesNijmegen/RMarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 23.9 KB
/
.Rhistory
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# ds[str_detect(ds$Crop,"beans")==T,]$Crop<-"Beans"
#
# ## Get rid of all the string ', Other'
# ds$Crop <- str_replace(ds$Crop,', Other','')
# ## Get rid of the all the strings ' and products'
# ds$Crop <- str_replace(ds$Crop,' and products','')
#
# ## now make a factor
# ds$Crop <- as.factor(ds$Crop)
#
# ## now use successive applications of the 'revalue' function to re-categorize things where I can't use a regex
# ds$Crop <- revalue(ds$Crop,c("Beer"="Alcohol", "Wine"="Alcohol", "Beverages, Fermented"="Alcohol"))
# ds$Crop <- ds$Crop <- revalue(ds$Crop,c("Barley"="Grains","Cereals - Excluding Beer"="Grains","Cereals"="Grains","Maize"="Grains","Millet"="Grains","Rice (Milled Equivalent)"="Grains","Oats"="Grains","Rye"="Grains","Sorghum"="Grains","Wheat"="Grains"))
# ds$Crop <- revalue(ds$Crop,c("Cephalopods"="Seafood", "Crustaceans"="Seafood", "Molluscs"="Seafood", "Aquatic Products"="Seafood"))
# ds$Crop <- revalue(ds$Crop,c("Sweeteners"="Sugar"))
# ds$Crop <- revalue(ds$Crop,c("Butter, Ghee"="Dairy", "Cream"="Dairy", "Milk - Excluding Butter"="Dairy"))
# ds$Crop <- revalue(ds$Crop,c("Cloves"="Spices", "Pepper"="Spices"))
# ds$Crop <- revalue(ds$Crop,c("Apples"="Fruit", "Bananas"="Fruit","Citrus"="Fruit", "Dates"="Fruit","Grapes (excl wine)"="Fruit", "Lemons, Limes"="Fruit","Oranges, Mandarines"="Fruit", "Pineapples"="Fruit"))
# ds$Crop <- revalue(ds$Crop,c("Aquatic Plants"="Veg", "Olives (including preserved)"="Veg","Onions"="Veg", "Palm kernels"="Veg","Peas"="Veg", "Pimento"="Veg","Tomatoes"="Veg","Vegetables"="Veg"))
# ds$Crop <- revalue(ds$Crop,c("Cassava"="Starch", "Plantains"="Starch", "Yams"="Starch"))
#
# write.table(ds,"FAO2.csv",row.names=F,sep=",")
# Here's a new chunk!
# Some notes: echo=FALSE means 'don't show the code in the output'
# In the R environment, a # is a comment. (in the markdown section, it means 'put in a heading'. try experimenting with # vs ## vs ### to see what this does.)
# Let's start by loading packages
library(dplyr)
library(ggplot2)
library(knitr) #for pretty output (includes function kable() )
# if you don't have the library installed, use install.packages('Package')
# install.packages('tidyr')
# easiest to do this in the console
# Let's load in some data and show it
ds <- read.csv('FAO2.csv')
#this command will work if the data are stored in the same folder
# let's actually use the data as a tibble rather than data frame-- nice for summarising
ds <- as_tibble(ds)
head(ds) # show the top of the data
#In the top of this chunk, we've asked all of the code and output to be hidden
#We did this by putting echo=FALSE in the top
# To figure out what's in the data, use this command
# levels(data$Column)
# What are the countries?
levels(ds$Area)
# Make a table of crops by items
# 't0 <- ' is base R. it means 'assign to object t0'
# The rest uses tidyr notation. %>% is a 'pipe' to pass operations onward.
# ds %>% means 'work within the data set ds, do some stuff to it'
# select means: put these colums in the output
# arrange means: order by crop, then within that, item
t1 <- ds %>%
select(Crop,Item) %>%
arrange(Crop,Item)
#unique is a base R function, it means take just the unique rows.
t1 <- unique(t1)
# print out in a pretty table
kable(t1)
# in this chunk I specified what the output size should be
# and said not to show the R code in the output
knitr::include_graphics("world-political-map.jpg")
# in this chunk, I asked the output to be shown with echo=TRUE
# Get the maximum value of a product, with function max().
# Need to set na.rm=T because there are NA values in the column.
m <- max(ds$KTonnes,na.rm=T)
# Then look up which area has that value for the column KTonnes
a <- ds[ds$KTonnes %in% m ,]$Area
# And which item it is
i <- ds[ds$KTonnes %in% m ,]$Item
# And which year!
y <- ds[ds$KTonnes %in% m ,]$Year
# Then, we can use those values in text by surrounding with `` and prefacing with r
# in this chunk, I asked everything to be shown with echo=TRUE
# Here are some useful tabulation commands. These use the tidyverse.
# This takes data and summarises for output.
# %>% is a 'pipe' and passes commands between lines.
# you can filter columns. here, we want columns where the item is sugar cane, used for food
t2 <- ds %>%
filter(Item=="Sugar cane") %>%
filter(Element=="Food")
t2
# you can use a | operator ('or') to look at multiple selections at once.
# you can use the select operator to only show some columns
# and the filter operator to remove columns even after summarising
# here we ask for only the crops of more than 10*1000 tonnes
t3<- ds %>%
filter(Area=="Netherlands" | Area=="Belgium") %>%
group_by(Area,Crop,Element,Year) %>%
summarise(TotalKTonnes = sum(as.numeric(KTonnes)) ) %>%
filter(TotalKTonnes > 10)
t3
# We can make lists and use these to filter.
# Here's list of some things eaten by people and animals
mixed <- c("Oats","Soyabeans", "Sugar cane")
# this will generate an error, but it actually works.
#an example of tidyverse 'lazy evaluation'
t4<- ds %>%
filter(Area=="Brazil" | Area=="Colombia") %>%
filter(Item==mixed)
t4
# finally, we can put these together to summarise.
# Here, we are taking mass of items grown in Brazil or Colombia
# and taking together the sum of all tonnes by year across rows
# (= total production in these countries, whether for food or feed)
# (Note that I used a less lazy evaluaton method here for the filter)
t5<- ds %>%
filter(Item %in% mixed == T) %>%
filter(Area=="Brazil" | Area=="Colombia") %>%
group_by(Year,Item) %>%
summarise(TotalKTonnes = sum(as.numeric(KTonnes)) )
t5
# Here's a new chunk!
# Some notes: echo=FALSE means 'don't show the code in the output'
# In the R environment, a # is a comment. (in the markdown section, it means 'put in a heading'. try experimenting with # vs ## vs ### to see what this does.)
# Let's start by loading packages
library(dplyr)
library(ggplot2)
library(knitr) #for pretty output (includes function kable() )
# if you don't have the library installed, use install.packages('Package')
# install.packages('tidyr')
# easiest to do this in the console
# Let's load in some data and show it
ds <- read.csv('FAO2.csv')
#this command will work if the data are stored in the same folder
# let's actually use the data as a tibble rather than data frame-- nice for summarising
ds <- as_tibble(ds)
head(ds) # show the top of the data
head(ds)
ds$Unit <- NULL
ds$Area.Code <- NULL
ds$ElementCode <- NULL
head(ds)
ds$Element.Code <- NULL
head(ds)
ds$Item.Code <- NULL
head(ds)
write.table(ds,"FAO2.csv",row.names=F,sep=",")
# ## Here are the data cleaning commands I ran.
# library(reshape2)
# library(plyr)
# library(stringr)
#
# ds <- read.csv('FAO.csv')
# ## In the original the year variable is what's known as 'wide', wheras the other variables are 'long'. Make year long too.
# ds <- melt(ds,measure.var=11:63,variable.name="Year",value.name="KTonnes")
# ## and make the year variable into a number, by taking off the Y and treating as a number
# ds$Year <- as.numeric(substr(ds$Year,2,5))
#
# ## Make a new variable that categorizes types of crops
# ## I am doing this in several steps.
# ds$Crop <- as.character(ds$Item)
# ## first find common strings and use regex to recode
# ds[str_detect(ds$Crop,"Fish")==T,]$Crop<-"Seafood"
# ds[str_detect(ds$Crop,"Oil")==T,]$Crop<-"Oil"
# ds[str_detect(ds$Crop,"Alcohol")==T,]$Crop<-"Alcohol"
# ds[str_detect(ds$Crop,"Meat")==T,]$Crop<-"Meat"
# ds[str_detect(ds$Crop,"meat")==T,]$Crop<-"Meat"
# ds[str_detect(ds$Crop,"Animal")==T,]$Crop<-"Meat"
# ds[str_detect(ds$Crop,"Offals")==T,]$Crop<-"Meat"
# ds[str_detect(ds$Crop,"Sugar")==T,]$Crop<-"Sugar"
# ds[str_detect(ds$Crop,"Nuts")==T,]$Crop<-"Nuts"
# ds[str_detect(ds$Crop,"nuts")==T,]$Crop<-"Nuts"
# ds[str_detect(ds$Crop,"Roots")==T,]$Crop<-"Roots"
# ds[str_detect(ds$Crop,"seed")==T,]$Crop<-"Seeds"
# ds[str_detect(ds$Crop,"fruit")==T,]$Crop<-"Fruit"
# ds[str_detect(ds$Crop,"Fruit")==T,]$Crop<-"Fruit"
# ds[str_detect(ds$Crop,"otatoes")==T,]$Crop<-"Starch"
# ds[str_detect(ds$Crop,"beans")==T,]$Crop<-"Beans"
#
# ## Get rid of all the string ', Other'
# ds$Crop <- str_replace(ds$Crop,', Other','')
# ## Get rid of the all the strings ' and products'
# ds$Crop <- str_replace(ds$Crop,' and products','')
#
# ## now make a factor
# ds$Crop <- as.factor(ds$Crop)
#
# ## now use successive applications of the 'revalue' function to re-categorize things where I can't use a regex
# ds$Crop <- revalue(ds$Crop,c("Beer"="Alcohol", "Wine"="Alcohol", "Beverages, Fermented"="Alcohol"))
# ds$Crop <- ds$Crop <- revalue(ds$Crop,c("Barley"="Grains","Cereals - Excluding Beer"="Grains","Cereals"="Grains","Maize"="Grains","Millet"="Grains","Rice (Milled Equivalent)"="Grains","Oats"="Grains","Rye"="Grains","Sorghum"="Grains","Wheat"="Grains"))
# ds$Crop <- revalue(ds$Crop,c("Cephalopods"="Seafood", "Crustaceans"="Seafood", "Molluscs"="Seafood", "Aquatic Products"="Seafood"))
# ds$Crop <- revalue(ds$Crop,c("Sweeteners"="Sugar"))
# ds$Crop <- revalue(ds$Crop,c("Butter, Ghee"="Dairy", "Cream"="Dairy", "Milk - Excluding Butter"="Dairy"))
# ds$Crop <- revalue(ds$Crop,c("Cloves"="Spices", "Pepper"="Spices"))
# ds$Crop <- revalue(ds$Crop,c("Apples"="Fruit", "Bananas"="Fruit","Citrus"="Fruit", "Dates"="Fruit","Grapes (excl wine)"="Fruit", "Lemons, Limes"="Fruit","Oranges, Mandarines"="Fruit", "Pineapples"="Fruit"))
# ds$Crop <- revalue(ds$Crop,c("Aquatic Plants"="Veg", "Olives (including preserved)"="Veg","Onions"="Veg", "Palm kernels"="Veg","Peas"="Veg", "Pimento"="Veg","Tomatoes"="Veg","Vegetables"="Veg"))
# ds$Crop <- revalue(ds$Crop,c("Cassava"="Starch", "Plantains"="Starch", "Yams"="Starch"))
#
#ds$Unit <- NULL
#ds$Area.Code <- NULL
#ds$Element.Code <- NULL
#ds$Item.Code <- NULL
# write.table(ds,"FAO2.csv",row.names=F,sep=",")
# Here's a new chunk!
# Some notes: echo=FALSE means 'don't show the code in the output'
# In the R environment, a # is a comment. (in the markdown section, it means 'put in a heading'. try experimenting with # vs ## vs ### to see what this does.)
# Let's start by loading packages
library(dplyr)
library(ggplot2)
library(knitr) #for pretty output (includes function kable() )
# if you don't have the library installed, use install.packages('Package')
# install.packages('tidyr')
# easiest to do this in the console
# Let's load in some data and show it
ds <- read.csv('FAO2.csv')
#this command will work if the data are stored in the same folder
# let's actually use the data as a tibble rather than data frame-- nice for summarising
ds <- as_tibble(ds)
head(ds) # show the top of the data
#In the top of this chunk, we've asked all of the code and output to be hidden
#We did this by putting echo=FALSE in the top
# To figure out what's in the data, use this command
# levels(data$Column)
# What are the countries?
levels(ds$Area)
# Make a table of crops by items
# 't0 <- ' is base R. it means 'assign to object t0'
# The rest uses tidyr notation. %>% is a 'pipe' to pass operations onward.
# ds %>% means 'work within the data set ds, do some stuff to it'
# select means: put these colums in the output
# arrange means: order by crop, then within that, item
t1 <- ds %>%
select(Crop,Item) %>%
arrange(Crop,Item)
#unique is a base R function, it means take just the unique rows.
t1 <- unique(t1)
# print out in a pretty table
kable(t1)
# in this chunk I specified what the output size should be
# and said not to show the R code in the output
knitr::include_graphics("world-political-map.jpg")
# in this chunk, I asked the output to be shown with echo=TRUE
# Get the maximum value of a product, with function max().
# Need to set na.rm=T because there are NA values in the column.
m <- max(ds$KTonnes,na.rm=T)
# Then look up which area has that value for the column KTonnes
a <- ds[ds$KTonnes %in% m ,]$Area
# And which item it is
i <- ds[ds$KTonnes %in% m ,]$Item
# And which year!
y <- ds[ds$KTonnes %in% m ,]$Year
# Then, we can use those values in text by surrounding with `` and prefacing with r
# in this chunk, I asked everything to be shown with echo=TRUE
# Here are some useful tabulation commands. These use the tidyverse.
# This takes data and summarises for output.
# %>% is a 'pipe' and passes commands between lines.
# you can filter columns. here, we want columns where the item is sugar cane, used for food
t2 <- ds %>%
filter(Item=="Sugar cane") %>%
filter(Element=="Food")
t2
# you can use a | operator ('or') to look at multiple selections at once.
# you can use the select operator to only show some columns
# and the filter operator to remove columns even after summarising
# here we ask for only the crops of more than 10*1000 tonnes
t3<- ds %>%
filter(Area=="Netherlands" | Area=="Belgium") %>%
group_by(Area,Crop,Element,Year) %>%
summarise(TotalKTonnes = sum(as.numeric(KTonnes)) ) %>%
filter(TotalKTonnes > 10)
t3
# We can make lists and use these to filter.
# Here's list of some things eaten by people and animals
mixed <- c("Oats","Soyabeans", "Sugar cane")
# this will generate an error, but it actually works.
#an example of tidyverse 'lazy evaluation'
t4<- ds %>%
filter(Area=="Brazil" | Area=="Colombia") %>%
filter(Item==mixed)
t4
# finally, we can put these together to summarise.
# Here, we are taking mass of items grown in Brazil or Colombia
# and taking together the sum of all tonnes by year across rows
# (= total production in these countries, whether for food or feed)
# (Note that I used a less lazy evaluaton method here for the filter)
t5<- ds %>%
filter(Item %in% mixed == T) %>%
filter(Area=="Brazil" | Area=="Colombia") %>%
group_by(Year,Item) %>%
summarise(TotalKTonnes = sum(as.numeric(KTonnes)) )
t5
# We can easily plot these with lines, points, and bars!
# we are using ggplot here.
#The first line sets up what the data are & what values are mapped to things
# in the plot ('aes' values).
# variables that are useful include: x, y values, color, lty (=line type)
# we can then add points or lines or bars with geom_point, geom_line, geom_bar
ggplot(data=t5,aes(x=Year,y=TotalKTonnes,color=Item))+
geom_point()
# add connector lines
# and make separate panels with facet_grid
#(explore what the ~ does by trying .~Area vs Area~.)
ggplot(data=t4,aes(x=Year,y=KTonnes,color=Item,lty=Element))+
geom_point()+
geom_line()+
facet_grid(.~Area)
# Here's an example of a bar plot
# And an example of user-specified figure height / width
ggplot(data=t3,aes(x=Year,y=TotalKTonnes,fill=Crop))+
geom_bar(stat='identity')+
facet_grid(Area~.)+
theme(legend.position="bottom")
#Most of what we did earlier was tibbles:
#this was a concious choice because the data structures were too big.
#Where 'tables' really shine is with summary data.
# here, we make a new variable with 'mutate'-- the last tibble 'verb'
t6 <- ds %>%
filter(Item %in% mixed == T) %>%
filter(Area=="Brazil" | Area=="Colombia") %>%
mutate(Decade=round(((Year-5)/10),0)*10) %>%
# year minus 5, divided by 10, rounded to 0 decimal places.
# (1969 - 5)/10=196.4, rounds to 1960
group_by(Decade,Item) %>%
summarise(TotalKTonnes = sum(as.numeric(KTonnes)) ) %>%
arrange(Item,Decade)
t6 <- as.data.frame(t6)
kable(t6)
#We can also restructure the table
# this uses matrix notation to paste together elements by column
# I've also put the units in tonnes again
kable(cbind(t6[1:6,1],t6[1:6,3]*1000,t6[7:12,3]*1000,t6[13:18,3]*1000),
col.names=c("Year","Tonnes/Oats","Tonnes/Soya","Tonnes/Sugar Cane"))
# ## Here are the data cleaning commands I ran.
# library(reshape2)
# library(plyr)
# library(stringr)
#
# ds <- read.csv('FAO.csv')
# ## In the original the year variable is what's known as 'wide', wheras the other variables are 'long'. Make year long too.
# ds <- melt(ds,measure.var=11:63,variable.name="Year",value.name="KTonnes")
# ## and make the year variable into a number, by taking off the Y and treating as a number
# ds$Year <- as.numeric(substr(ds$Year,2,5))
#
# ## Make a new variable that categorizes types of crops
# ## I am doing this in several steps.
# ds$Crop <- as.character(ds$Item)
# ## first find common strings and use regex to recode
# ds[str_detect(ds$Crop,"Fish")==T,]$Crop<-"Seafood"
# ds[str_detect(ds$Crop,"Oil")==T,]$Crop<-"Oil"
# ds[str_detect(ds$Crop,"Alcohol")==T,]$Crop<-"Alcohol"
# ds[str_detect(ds$Crop,"Meat")==T,]$Crop<-"Meat"
# ds[str_detect(ds$Crop,"meat")==T,]$Crop<-"Meat"
# ds[str_detect(ds$Crop,"Animal")==T,]$Crop<-"Meat"
# ds[str_detect(ds$Crop,"Offals")==T,]$Crop<-"Meat"
# ds[str_detect(ds$Crop,"Sugar")==T,]$Crop<-"Sugar"
# ds[str_detect(ds$Crop,"Nuts")==T,]$Crop<-"Nuts"
# ds[str_detect(ds$Crop,"nuts")==T,]$Crop<-"Nuts"
# ds[str_detect(ds$Crop,"Roots")==T,]$Crop<-"Roots"
# ds[str_detect(ds$Crop,"seed")==T,]$Crop<-"Seeds"
# ds[str_detect(ds$Crop,"fruit")==T,]$Crop<-"Fruit"
# ds[str_detect(ds$Crop,"Fruit")==T,]$Crop<-"Fruit"
# ds[str_detect(ds$Crop,"otatoes")==T,]$Crop<-"Starch"
# ds[str_detect(ds$Crop,"beans")==T,]$Crop<-"Beans"
#
# ## Get rid of all the string ', Other'
# ds$Crop <- str_replace(ds$Crop,', Other','')
# ## Get rid of the all the strings ' and products'
# ds$Crop <- str_replace(ds$Crop,' and products','')
#
# ## now make a factor
# ds$Crop <- as.factor(ds$Crop)
#
# ## now use successive applications of the 'revalue' function to re-categorize things where I can't use a regex
# ds$Crop <- revalue(ds$Crop,c("Beer"="Alcohol", "Wine"="Alcohol", "Beverages, Fermented"="Alcohol"))
# ds$Crop <- ds$Crop <- revalue(ds$Crop,c("Barley"="Grains","Cereals - Excluding Beer"="Grains","Cereals"="Grains","Maize"="Grains","Millet"="Grains","Rice (Milled Equivalent)"="Grains","Oats"="Grains","Rye"="Grains","Sorghum"="Grains","Wheat"="Grains"))
# ds$Crop <- revalue(ds$Crop,c("Cephalopods"="Seafood", "Crustaceans"="Seafood", "Molluscs"="Seafood", "Aquatic Products"="Seafood"))
# ds$Crop <- revalue(ds$Crop,c("Sweeteners"="Sugar"))
# ds$Crop <- revalue(ds$Crop,c("Butter, Ghee"="Dairy", "Cream"="Dairy", "Milk - Excluding Butter"="Dairy"))
# ds$Crop <- revalue(ds$Crop,c("Cloves"="Spices", "Pepper"="Spices"))
# ds$Crop <- revalue(ds$Crop,c("Apples"="Fruit", "Bananas"="Fruit","Citrus"="Fruit", "Dates"="Fruit","Grapes (excl wine)"="Fruit", "Lemons, Limes"="Fruit","Oranges, Mandarines"="Fruit", "Pineapples"="Fruit"))
# ds$Crop <- revalue(ds$Crop,c("Aquatic Plants"="Veg", "Olives (including preserved)"="Veg","Onions"="Veg", "Palm kernels"="Veg","Peas"="Veg", "Pimento"="Veg","Tomatoes"="Veg","Vegetables"="Veg"))
# ds$Crop <- revalue(ds$Crop,c("Cassava"="Starch", "Plantains"="Starch", "Yams"="Starch"))
#
#ds$Unit <- NULL
#ds$Area.Code <- NULL
#ds$Element.Code <- NULL
#ds$Item.Code <- NULL
# write.table(ds,"FAO2.csv",row.names=F,sep=",")
# Here's a new chunk!
# Some notes: echo=FALSE means 'don't show the code in the output'
# In the R environment, a # is a comment. (in the markdown section, it means 'put in a heading'. try experimenting with # vs ## vs ### to see what this does.)
# Let's start by loading packages
library(dplyr)
library(ggplot2)
library(knitr) #for pretty output (includes function kable() )
# if you don't have the library installed, use install.packages('Package')
# install.packages('tidyr')
# easiest to do this in the console
# Let's load in some data and show it
ds <- read.csv('FAO2.csv')
#this command will work if the data are stored in the same folder
# let's actually use the data as a tibble rather than data frame-- nice for summarising
ds <- as_tibble(ds)
head(ds) # show the top of the data
#In the top of this chunk, we've asked all of the code and output to be hidden
#We did this by putting echo=FALSE in the top
# To figure out what's in the data, use this command
# levels(data$Column)
# What are the countries?
levels(ds$Area)
# Make a table of crops by items
# 't0 <- ' is base R. it means 'assign to object t0'
# The rest uses tidyr notation. %>% is a 'pipe' to pass operations onward.
# ds %>% means 'work within the data set ds, do some stuff to it'
# select means: put these colums in the output
# arrange means: order by crop, then within that, item
t1 <- ds %>%
select(Crop,Item) %>%
arrange(Crop,Item)
#unique is a base R function, it means take just the unique rows.
t1 <- unique(t1)
# print out in a pretty table
kable(t1)
# in this chunk I specified what the output size should be
# and said not to show the R code in the output
knitr::include_graphics("world-political-map.jpg")
# in this chunk, I asked the output to be shown with echo=TRUE
# Get the maximum value of a product, with function max().
# Need to set na.rm=T because there are NA values in the column.
m <- max(ds$KTonnes,na.rm=T)
# Then look up which area has that value for the column KTonnes
a <- ds[ds$KTonnes %in% m ,]$Area
# And which item it is
i <- ds[ds$KTonnes %in% m ,]$Item
# And which year!
y <- ds[ds$KTonnes %in% m ,]$Year
# Then, we can use those values in text by surrounding with `` and prefacing with r
# in this chunk, I asked everything to be shown with echo=TRUE
# Here are some useful tabulation commands. These use the tidyverse.
# This takes data and summarises for output.
# %>% is a 'pipe' and passes commands between lines.
# you can filter columns. here, we want columns where the item is sugar cane, used for food
t2 <- ds %>%
filter(Item=="Sugar cane") %>%
filter(Element=="Food")
t2
# you can use a | operator ('or') to look at multiple selections at once.
# you can use the select operator to only show some columns
# and the filter operator to remove columns even after summarising
# here we ask for only the crops of more than 10*1000 tonnes
t3<- ds %>%
filter(Area=="Netherlands" | Area=="Belgium") %>%
group_by(Area,Crop,Element,Year) %>%
summarise(TotalKTonnes = sum(as.numeric(KTonnes)) ) %>%
filter(TotalKTonnes > 10)
t3
# We can make lists and use these to filter.
# Here's list of some things eaten by people and animals
mixed <- c("Oats","Soyabeans", "Sugar cane")
# this will generate an error, but it actually works.
#an example of tidyverse 'lazy evaluation'
t4<- ds %>%
filter(Area=="Brazil" | Area=="Colombia") %>%
filter(Item==mixed)
t4
# finally, we can put these together to summarise.
# Here, we are taking mass of items grown in Brazil or Colombia
# and taking together the sum of all tonnes by year across rows
# (= total production in these countries, whether for food or feed)
# (Note that I used a less lazy evaluaton method here for the filter)
t5<- ds %>%
filter(Item %in% mixed == T) %>%
filter(Area=="Brazil" | Area=="Colombia") %>%
group_by(Year,Item) %>%
summarise(TotalKTonnes = sum(as.numeric(KTonnes)) )
t5
# We can easily plot these with lines, points, and bars!
# we are using ggplot here.
#The first line sets up what the data are & what values are mapped to things
# in the plot ('aes' values).
# variables that are useful include: x, y values, color, lty (=line type)
# we can then add points or lines or bars with geom_point, geom_line, geom_bar
ggplot(data=t5,aes(x=Year,y=TotalKTonnes,color=Item))+
geom_point()
# add connector lines
# and make separate panels with facet_grid
#(explore what the ~ does by trying .~Area vs Area~.)
ggplot(data=t4,aes(x=Year,y=KTonnes,color=Item,lty=Element))+
geom_point()+
geom_line()+
facet_grid(.~Area)
# Here's an example of a bar plot
# And an example of user-specified figure height / width
ggplot(data=t3,aes(x=Year,y=TotalKTonnes,fill=Crop))+
geom_bar(stat='identity')+
facet_grid(Area~.)+
theme(legend.position="bottom")
#Most of what we did earlier was tibbles:
#this was a concious choice because the data structures were too big.
#Where 'tables' really shine is with summary data.
# here, we make a new variable with 'mutate'-- the last tibble 'verb'
t6 <- ds %>%
filter(Item %in% mixed == T) %>%
filter(Area=="Brazil" | Area=="Colombia") %>%
mutate(Decade=round(((Year-5)/10),0)*10) %>%
# year minus 5, divided by 10, rounded to 0 decimal places.
# (1969 - 5)/10=196.4, rounds to 1960
group_by(Decade,Item) %>%
summarise(TotalKTonnes = sum(as.numeric(KTonnes)) ) %>%
arrange(Item,Decade)
t6 <- as.data.frame(t6)
kable(t6)
#We can also restructure the table
# this uses matrix notation to paste together elements by column
# I've also put the units in tonnes again
kable(cbind(t6[1:6,1],t6[1:6,3]*1000,t6[7:12,3]*1000,t6[13:18,3]*1000),
col.names=c("Year","Tonnes/Oats","Tonnes/Soya","Tonnes/Sugar Cane"))
install.packages('effects')
install.packages('kableExtra')
knitr::opts_chunk$set(echo = FALSE)
knitr::include_graphics("RMarkdown.png") # I can insert a picture which is in the wd
knitr::include_graphics("newRMD.png")
knitr::include_graphics("newRMD.png")