-
Notifications
You must be signed in to change notification settings - Fork 0
/
supporting_datasets.R
246 lines (177 loc) · 9.81 KB
/
supporting_datasets.R
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
# #
# #
# FAO - Fisheries #
# Producing supporting datasets #
# #
# #
# #
###############################################################################
# Loading libraries
## Note: if it is the first time: install.packages() first
library(dplyr) # For data cleaning (wrangling)
library(stringr) # For string manipulation (data cleaning)
message("starting supporting_datasets.R")
## Food matching: Fish and Fisheries ----
# Preparing data frame for replicating the Global FCT (only fish)
# contains information for each fish on ISSCAAP code, ICS FAOSTAT fish codes, and
# alpha-three code, when available.
#├ FAO data - ICS codes and ISSCAAP groups ----
#reading excel
readxl::excel_sheets(here::here("data",
"List_SUA_ICS_fish.xlsx"))
#loading the data
ics_code <- readxl::read_excel(here::here("data",
"List_SUA_ICS_fish.xlsx"))%>%
janitor::clean_names() %>% #tidying colnames
filter(!is.na(ics_faostat_sua_english_description)) #removing empty rows
#Fixing a typo
ics_code$ics_faostat_sua_english_description <- gsub( "frozen, fillet", ", frozen fillet", ics_code$ics_faostat_sua_english_description)
#Checking categories w/o processing code
#Aquatic mammals and aquatic plants
ics_code %>% mutate(
product_type = case_when(
str_detect(ics_faostat_sua_english_description, "fresh fillets") ~ "3",
str_detect(ics_faostat_sua_english_description, "frozen fillets") ~ "4",
str_detect(ics_faostat_sua_english_description, "fresh") ~ "1",
str_detect(ics_faostat_sua_english_description, "frozen") ~ "2",
str_detect(ics_faostat_sua_english_description, "cured") ~ "5",
str_detect(ics_faostat_sua_english_description, "canned") ~ "6",
str_detect(ics_faostat_sua_english_description, "preparations") ~ "7",
str_detect(ics_faostat_sua_english_description, "body oils") ~ "8",
str_detect(ics_faostat_sua_english_description, "liver oils") ~ "9",
TRUE ~ "NA"
)
) %>% filter(product_type == "NA") %>%
pull(ics_faostat_sua_english_description, isscaap_group)
#adding processing group no. (product_type) to ics file
#order should be from the most specific to the least
ics_code <- ics_code %>% mutate(
product_type = case_when(
str_detect(ics_faostat_sua_english_description, "fresh fillets") ~ "3",
str_detect(ics_faostat_sua_english_description, "frozen fillets") ~ "4",
str_detect(ics_faostat_sua_english_description, "fresh") ~ "1",
str_detect(ics_faostat_sua_english_description, "frozen") ~ "2",
str_detect(ics_faostat_sua_english_description, "cured") ~ "5",
str_detect(ics_faostat_sua_english_description, "canned") ~ "6",
str_detect(ics_faostat_sua_english_description, "preparations") ~ "7",
str_detect(ics_faostat_sua_english_description, "body oils") ~ "8",
str_detect(ics_faostat_sua_english_description, "liver oils") ~ "9",
TRUE ~ "NA"
)
) %>% relocate(product_type, .before = "ics_faostat_sua_english_description")
#├ Saving ICS, ISSCAAP groups & product type info (fish matching) ----
saveRDS(ics_code, file = here::here("inter-output", "ics-code.RDS"))
#├ Loading the file ----
ics_code_file <- read.csv(here::here("data", "ics-code_fish-code.csv")) %>%
rename(source_fct = "Source.FCT.for.NVs") #renaming variable the FCT source (e.g. BA13, WA19)
#├ Standardising the file for FCT standard fdc_id ----
#fixing discrepancy between fcd_id in our dataframe (df) and Global FCT df for KE18 and US19
#This is needed for merging and filtering the fish and adding the ICS FAOSTAT code
ics_code_file %>% filter(str_detect(fdc_id, "^0"))
ics_code_file <- ics_code_file %>%
mutate(fdc_id = ifelse(source_fct == "KE18",
str_replace(fdc_id, "^0", ""), fdc_id)) # %>% #removing the 0 of the fdc_id
# mutate(fdc_id = ifelse(source_fct == "US19",
# NDB_number , fdc_id)) #using NDB_number as the fdc_id
#checking the US19 data from the FAO Global Fisheries data
ics_code_file %>% filter(source_fct == "US19")
#├ Updating fish matches ----
## Following expert advise (2023)
names(ics_code_file)
ics_code_file$Food.description[ics_code_file$ICS.FAOSTAT.SUA.Current.Code == "1532"]
#├├ Excluding: ----
# Excluded, not fresh fish (surimi)
ics_code_file$ICS.FAOSTAT.SUA.Current.Code[ics_code_file$ICS.FAOSTAT.SUA.Current.Code %in% c("1514", "1515") &
ics_code_file$fdc_id == "10200"]
ics_code_file <- subset(ics_code_file, !(ICS.FAOSTAT.SUA.Current.Code %in% c("1514", "1515") & fdc_id == "10200"))
# Excluded, frozen and not fresh fish
ics_code_file$ICS.FAOSTAT.SUA.Current.Code[ics_code_file$ICS.FAOSTAT.SUA.Current.Code %in% c("1503", "15030") &
ics_code_file$fdc_id == "091019"]
ics_code_file <- subset(ics_code_file, !(ICS.FAOSTAT.SUA.Current.Code %in% c("1503", "15030") & fdc_id == "091019"))
# Duplicated item!
ics_code_file$ICS.FAOSTAT.SUA.Current.Code[ics_code_file$ICS.FAOSTAT.SUA.Current.Code %in% c("1504") &
ics_code_file$fdc_id == "091019"]
subset(ics_code_file, (ICS.FAOSTAT.SUA.Current.Code %in% c("1504") & fdc_id == "091019"))
# Removing dulpicated (15)
dim(ics_code_file)
ics_code_file <- distinct(ics_code_file)
# Excluded, fish not specified in the description - Not found
#ics_code_file$ICS.FAOSTAT.SUA.Current.Code[ics_code_file$ICS.FAOSTAT.SUA.Current.Code %in% c("1532") &
# ics_code_file$fdc_id == "9003"]
# Excluded, recipe with many ingredients
ics_code_file$ICS.FAOSTAT.SUA.Current.Code[ics_code_file$ICS.FAOSTAT.SUA.Current.Code %in% c("1554") &
ics_code_file$fdc_id == "15142"]
ics_code_file <- subset(ics_code_file, !(ICS.FAOSTAT.SUA.Current.Code %in% c("1554") & fdc_id == "15142"))
# Excluded, refers to fat only
ics_code_file$ICS.FAOSTAT.SUA.Current.Code[ics_code_file$ICS.FAOSTAT.SUA.Current.Code %in% c("1580") &
ics_code_file$fdc_id == "11112"]
ics_code_file <- subset(ics_code_file, !(ICS.FAOSTAT.SUA.Current.Code %in% c("1580") & fdc_id == "11112"))
#Excluded food from the matching for the four SUA items - Not found
#ics_code_file$fdc_id[ICS.FAOSTAT.SUA.Current.Code$ICS.FAOSTAT.SUA.Current.Code %in% c("1505","15051","1518","1544") &
# ics_code_file$fdc_id == "173712"]
#ics_code_file <- subset(ics_code_file, !(ICS.FAOSTAT.SUA.Current.Code %in% c("1580") & fdc_id == "11112"))
#
#ics_code_file$source_fct[ics_code_file$ICS.FAOSTAT.SUA.Current.Code %in% c("1573")]
## Identification of the ICS fish category in the ICS file
#├ Saving ICS and food_id (fish matching) ----
saveRDS(ics_code_file, file = here::here("inter-output", "ics-code_fish-code.RDS"))
## Edible portion: Fish and Fisheries ----
# Loading the data
#Original 122 columns
fao_fish <- read.csv(here::here("data", "FISHERIES-GlobalNCT_ForSharing_Feb2022.csv"))
# Checking the dataset
dim(fao_fish)
names(fao_fish)
names(fao_fish[, c(1:122)])
fao_fish[c(1:2), c(1:122)]
fao_fish[2, c(13:30)]
which(is.na(fao_fish[1, c(1:122)]))
which(fao_fish[1, c(1:122)] == "")
fao_fish[1, c(1:30)]
# Renaming variables
n <- which(fao_fish[1, c(1:122)] == "")
which(fao_fish[2, c(1:122)] == "Quality rating for food match")
fao_fish[1, n]
fao_fish[1, c(1:122)[!c(1:122) %in% n]]
#Renaming ICS codes variables
names(fao_fish)[1:6] <- fao_fish[2, c(1:6)]
#Renaming nutrients
names(fao_fish)[(7:122)[!c(7:122) %in% n]] <- fao_fish[1, c(7:122)[!c(7:122) %in% n]]
names(fao_fish)
# Getting info on variable: "Edible coefficient to be used" to be added to our dataset
which(fao_fish[2, c(1:122)] == "Edible coefficient to be used")
n <- which(fao_fish[, 27] != "")
#There is one duplicated - 1509
dim(fao_fish[n, c(1,27)])
dim(unique(fao_fish[n, c(1,27)]))
edible_ics <- unique(fao_fish[n, c(1,27)])
edible_ics[,1] <- paste("SUMMARY ROW -", edible_ics[,1])
#├ Save data set ----
# "Edible coefficient to be used" to a R file - for formatting.
saveRDS(edible_ics[c(2:96),],
file = "inter-output/edible_coefficient.RDS")
# Getting the info for each ICS FAOSTAT category info (n=95)
#there seems to be duplicates (n=104)
unique(fao_fish[!is.na(fao_fish$`Food description`),c(1:6)])
#Checking duplicated
duplicated(unique(fao_fish[!is.na(fao_fish$`Food description`),c(1:6)])[1])
#Sequence of no. seems like a typo from excel
unique(fao_fish[!is.na(fao_fish$`Food description`),c(1:6)])[c(32:40),]
#Wrong code for one item
unique(fao_fish[!is.na(fao_fish$`Food description`),c(1:6)])[c(85:87),]
# Correcting column 5 () - 1518 == 15180 (were coded as seq. 15180:15190)
fao_fish[,5][fao_fish[,1] == "1518"] <- 15180
# Correcting column 5 () - 1557 == 15570 (was coded as 15530)
fao_fish[,5][fao_fish[,1] == "1557"] <- 15570
# Checking duplicates w/i each category
#fao_fish %>% group_by(`ICS FAOSTAT SUA Current Code`) %>%
# count(Source, fdc_id) %>%
# filter(n>1) %>%
# arrange(desc(n))
#Save this one to complete the FAO table.
unique(fao_fish[!is.na(fao_fish$`Food description`),c(1:6)])
n <- nrow(unique(fao_fish[!is.na(fao_fish$`Food description`),c(1:6)]))
unique(fao_fish[!is.na(fao_fish$`Food description`),c(1:6)])[c(3:n),]
# Save ICS info to a R file - for formatting.
saveRDS(unique(fao_fish[!is.na(fao_fish$`Food description`),c(1:6)])[c(3:n),],
file = "inter-output/fao-ics-desc.RDS")