forked from micronutrientsupport/fct
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMAPS_Dictionary-Protocol.R
8171 lines (6413 loc) · 257 KB
/
MAPS_Dictionary-Protocol.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(tidyverse)
library(fuzzyjoin)
####================== LOADING DATASET =============================####
#
#
# fbs <- read.csv(here::here('data',
# 'Simplified-match-FBS-region_v1.5.csv'))
#
# fct <- readxl::read_xlsx(here::here("data", 'Food.item.dictionary.MAPS.v0.3.xlsx'),
# sheet = "S Table 2")
#
# fbs2013 <- read.csv(here::here( 'data', 'FBS_africa-region_2013_2021-02-11.csv'))
#
# dictionary <- read.csv(here::here('MAPS_Dictionary_03.csv'))
#
# fgroups <- read.csv(here::here('EJ_Foodgroup.csv')) %>% select(1:2)
#
#
#
##----- creating the food-dictionary-----
#Searching terms and correspondences with categories - use CPC
#http://datalab.review.fao.org/datalab/caliper/web/concepts-search
#Updating MAPS-Dictionary with EJ food groups
#dataset with the food groups and names
#group <- readr::read_csv("FoodName_0, ID_0
#Animal Products, AP
#Cereals, CE
#Fruits and Vegetables, FV
#Pulses and Beans, PB
#Roots and Tubers, RT
#Other foods, OT")
#
##Using fuzzy join to join column due to slight differences in names
## e.g., removed "and products" in fgroups dataset
#
#dict_group <- dictionary %>%
# regex_left_join(fgroups, by = c(FoodName_1 = "Item.FBS"))
#
#dict_group <- dict_group %>%
# left_join(fgroups, by = c(FoodName_1 = "Item.FBS"))
#
#dict_group <- dict_group %>%
# mutate(Foodgroup = ifelse(!is.na(Foodgroup.y),
# Foodgroup.y, Foodgroup.x))
#
##Select and rename variables of interest
#dict_group <- dict_group %>%
# select(-c(ID_0, FoodName_0)) %>%
# rename(ID_0 = "Foodgroup") %>%
# select(-c(starts_with("Foodgroup")))
#
##joining names and group code
#dict_group <- left_join(dict_group, group)
##reordering variables
#dict_group <- dict_group %>%
# relocate(c("ID_0", "FoodName_0"), .before = ID_1) %>%
# mutate_at("FoodName_3", str_to_lower)
#
##saving csv
#dict_group %>%
# write.csv(here::here("MAPS_Dictionary_v1.0.csv"))
#
#### --------------- Creating a template for data collection-----
#
##fbs <- fbs %>% filter(!is.na(Item)) %>% select(!starts_with("X"))
#
#write.csv(fbs, here::here('Simplified-match-FBS-region_2021-02-10.csv'))
#### --------------- Creating a template for code and name tags----
#
#fbs_tag <- fbs %>%
# rename(foodname_fbs = 'Item.FBS.category') %>%
# select( 'foodname_fbs', 'code_3', 'foodname_3', 'Region') %>%
# filter(str_detect(code_3, "[^[:alnum:] ]"))
#
#
#fct1 <- left_join(fct, fbs_tag,
# by = c("Item (FBS category)" = "foodname_fbs"))
#
#fct1 <- fct1 %>% distinct(Item, .keep_all = TRUE)
#
##fct2 <- fct1 %>% group_split(Region)
#
##DONT RUN THIS!-----
#x <- fct1 %>% group_by(Region) %>%
# select(Region,
# Nutrient,
# `Fitted item (as decribed in published source)`:foodname_3.y) %>%
# unite("Nutrient", c("Nutrient", "Unit"), sep = "_") %>%
# pivot_wider(names_from = Nutrient,
# values_from = `Concentration (100 g-1 fresh weight)`) %>%
# group_split(Region)
#
########################
#
#x <- fct1 %>%
# rename(code03 = 'code_3.y',
# foodname03 = "foodname_3.y",
# foodname_originalfct = "Fitted item (as decribed in published source)") %>%
# select(Region,
# Nutrient,
# foodname_originalfct:foodname03) %>%
# unite("Nutrient", c("Nutrient", "Unit"), sep = "_") %>%
# pivot_wider(names_from = Nutrient,
# values_from = `Concentration (100 g-1 fresh weight)`,
# values_fn = mean) #when multiple values were collated
##in a list give the mean
#
## x$foodname03 <- str_to_lower(x$foodname03)
##
## x <- left_join(x, dict_group, by = c("code03" = "FE2_3",
## "foodname03" = "FoodName_3"))
##
# x %>% filter(!is.na(code03)) %>%
# write.csv(here::here("regional-fct_2021-02-22.csv"))
#
#
####-----New Dictionary codes --------#####
dictionary.df <- read.csv(here::here("metadata", "MAPS_Dictionary_v2.6.csv")) %>%
select(-starts_with("X"))
dictionary.df$scientific_name <- NA
colnames(dictionary.df)
dictionary.df %>% filter(str_detect(ID_3, "01520"))
#data corrections for dictionary.df, dict.comp and TZ_data . Seems like there were some spelling mistakes that lead to broken matches.
#dictionary.df <- dictionary.df %>% drop_na(ID_0) # not needed, but maybe a good idea - quite a few all NA rows?
dictionary.df$ID_3[dictionary.df$ID_3 == "01520.01.01"] <- "1520.01.01"
dictionary.df$ID_3[dictionary.df$ID_3 == "01520.01.02"] <- "1520.01.02"
dictionary.df$ID_3[dictionary.df$ID_3 == "01520.01.03"] <- "1520.01.03"
#It was a typo that duplicated two contiguous food items,
#here we are solving it
dictionary.df$ID_3[dictionary.df$FoodName_3 == "cake, banana"] <- "F0022.07"
#---tracking-issue-2
#checking ids duplication at group level 2 (ID_2):
dictionary.df %>% distinct(FoodName_2, ID_2) %>%
count(ID_2) %>% arrange(desc(n))
#checking what items with duplicated ids
dictionary.df %>% filter(ID_2 %in% c("1510", "1530"))
#checking that the new proposed ids
dictionary.df %>% filter(ID_2 %in% c("15100", "15300"))
#changing fish ids to new ones:
dictionary.df <- dictionary.df %>% mutate(ID_2 = case_when(
FoodName_2 == "freshwater fish, liver oil" ~ "15100",
FoodName_2 == "pelagic fish, frozen, fillet" ~ "15300",
TRUE ~ ID_2
))
#New codes coming from FBS and FBSH
#FBS
#2558 == 1442.01 (mustard seed, dried, raw) New
# 2659 == 24110.01 (alcohol, 80perc) New
#FBSH
#2562 == 1491.02.01 (palm kernel, raw) New
dictionary.df <- dictionary.df %>%
# add_row(
# ID_0 = "OT",
# FoodName_0 = "Other foods",
# ID_1 = 2558,
# FoodName_1 = "rape and mustardseed and products",
# ID_2 = "1442",
# FoodName_2 = "mustard seed",
# ID_3 = "1442.01",
# FE2_3 = "A015S#F28.A07KG$F28.A07HS",
# FoodName_3 = "mustard seed, dried, raw") %>%
# add_row(
# ID_0 = "OT",
# FoodName_0 = "Other foods",
# ID_1 = 2659,
# FoodName_1 = "alcohol, non-food and products",
# ID_2 = "24110",
# FoodName_2 = "undenatured ethyl alcohol of an alcoholic strength by volume of 80% vol or higher",
# ID_3 = "24110.01",
# FE2_3 = NA,
# FoodName_3 = "alcohol, 80perc") %>%
# add_row(
# ID_0 = "OT",
# FoodName_0 = "Other foods",
# ID_1 = 2562,
# FoodName_1 = "palm kernels and products",
# ID_2 = "1491.02",
# FoodName_2 = "palm kernels [only calories]",
# ID_3 = "1491.02.01",
# FE2_3 = "A0DAJ#F28.A07HS",
# FoodName_3 = " palm kernel, raw") %>%
add_row(
ID_0 = "FV",
FoodName_0 = "Fruits and Vegetables",
ID_1 = 2617,
FoodName_1 = "apples and products",
ID_2 = "21435.01",
FoodName_2 = "apple juice",
ID_3 = "21435.01.01",
FE2_3 = "A039M#F10.A077J", #We used qualitative info w/ added sugar to specify that it's sweetened
FoodName_3 = " apple juice, sweetened") #we did not used the #sweetened agent bc we did not know
#the sweetened used.
#making a distinction between two rice that were named the same
#we will use local rice whenever possible
#default rice white == "23161.02.01"
dictionary.df$FoodName_3[dictionary.df$ID_3 == "23161.01.01"] <- "rice grain, imported, white, dried, raw"
dictionary.df$FoodName_3[dictionary.df$ID_3 == "23161.02.01"] <- "rice grain, local, white, dried, raw"
#Found a issue with some ID_0 classification
#Need to change them to oil and seeds.
#PB Pulses and Beans "2561", sesame seed and products
#PB Pulses and Beans "2571", soyabean oil and products
#PB Pulses and Beans "2572", groundnut oil and products
#PB Pulses and Beans "2579", sesameseed oil and products
#Found a issue with some ID_0 classification
#Need to change them to Other foods.
#PB Pulses and Beans 2561 sesame seed and products
#PB Pulses and Beans 2571 soyabean oil and products
#PB Pulses and Beans 2572 groundnut oil and products
#PB Pulses and Beans 2579 sesameseed oil and products
#checking what items with duplicated ids
dictionary.df %>% filter(ID_1 %in% c("2561",
"2571",
"2572",
"2579"))
dictionary.df$ID_0[dictionary.df$ID_1 %in% c("2561", "2571",
"2572","2579")] <- "OT"
dictionary.df$FoodName_0[dictionary.df$ID_1 %in% c("2561", "2571",
"2572","2579")] <- "Other foods"
#We are adding canned beans, we need to decide where to put them
#Since they are not dried and they are preserved, acc. to FAO
#logic it should be under other vegetables.
#additionally we are adding them to be matched to tinned vegetables, in
#ihs5....
#food-group fixing
#sugar
dictionary.df$FoodName_0[dictionary.df$ID_3 == "23511.02.01"] <- "Other foods"
dictionary.df$ID_0[dictionary.df$ID_3 == "23511.02.01"] <- "OT"
#juice
dictionary.df$FoodName_2[dictionary.df$ID_2 == "21435.01"]
dictionary.df$FoodName_1[dictionary.df$ID_2 == "21435.01"]
dictionary.df$FoodName_0[dictionary.df$ID_2 == "21435.01"]
#Adding new entries from Ethiopia HCES - ess3
dictionary.df <- dictionary.df %>%
add_row(
ID_0 = "CE",
FoodName_0 = "Cereals",
ID_1 = 2513,
FoodName_1 = "barley and products",
ID_2 = "115",
FoodName_2 = "barley",
ID_3 = "115.01",
FE2_3 = "",
FoodName_3 = "barley grain, dried, unrefined, raw") %>%
add_row(
ID_0 = "PB",
FoodName_0 = "Pulses and Beans",
ID_1 = 2549,
FoodName_1 = "pulses, other and products",
ID_2 = "1703",
FoodName_2 = "chick peas, dry",
ID_3 = "1703.01",
FE2_3 = "",
FoodName_3 = "chick peas, dried, raw") %>%
add_row(
ID_0 = "OT",
FoodName_0 = "Other foods",
ID_1 = 2570,
FoodName_1 = "oilcrops, other and products",
ID_2 = "1449.9",
FoodName_2 = "other oil seeds, n.e.c.",
ID_3 = "1449.9.01",
FE2_3 = "",
FoodName_3 = "niger seeds, dried, raw") %>%
add_row(
ID_0 = "RT",
FoodName_0 = "Roots and Tubers",
ID_1 = 2534,
FoodName_1 = "roots, other and products",
ID_2 = "23170.02",
FoodName_2 = "flour of roots and tubers nes",
ID_3 = "23170.02.01",
FE2_3 = "",
FoodName_3 = "bread, ensete pulp, fermented, raw",
Description1 = "kocho: bread-like fermented food made from chopped and grated ensete pulp",
Desc1.ref = "https://en.wikipedia.org/wiki/Kocho_(food)") %>%
add_row(
ID_0 = "RT",
FoodName_0 = "Roots and Tubers",
ID_1 = 2534,
FoodName_1 = "roots, other and products",
ID_2 = "23170.02",
FoodName_2 = "flour of roots and tubers nes",
ID_3 = "23170.02.02",
FE2_3 = "",
FoodName_3 = "ensete, flour, raw",
Description1 = "bula") %>%
add_row(
ID_0 = "CE",
FoodName_0 = "Cereals",
ID_1 = 2520,
FoodName_1 = "cereals, other and products",
ID_2 = "23140.08",
FoodName_2 = "cereal preparations",
ID_3 = "23140.08.01",
FE2_3 = "",
FoodName_3 = "injera, teff grain, ready-to-eat")
#Adding new entries from Ethiopia HCES - ESS4
dictionary.df <- dictionary.df %>%
add_row(
ID_0 = "PB",
FoodName_0 = "Pulses and Beans",
ID_1 = 2549,
FoodName_1 = "pulses, other and products",
ID_2 = "1709.01",
FoodName_2 = "vetches",
ID_3 = "1709.01.01",
FE2_3 = "",
FoodName_3 = "vetch, dried, raw") %>%
add_row(
ID_0 = "OT",
FoodName_0 = "Other foods",
ID_1 = 2645,
FoodName_1 = "spices, other and products",
ID_2 = "1699",
FoodName_2 = "other stimulant, spice and aromatic crops, n.e.c.",
ID_3 = "1699.07",
FE2_3 = "",
FoodName_3 = "fenugreek, dried, raw") %>%
add_row(
ID_0 = "PB",
FoodName_0 = "Pulses and Beans",
ID_1 = 2549,
FoodName_1 = "pulses, other and products",
ID_2 = "1709.9",
FoodName_2 = "other pulses n.e.c.",
ID_3 = "1709.9.01",
FE2_3 = "",
FoodName_3 = "mung bean, dried, raw") %>%
add_row(
ID_0 = "PB",
FoodName_0 = "Pulses and Beans",
ID_1 = 2549,
FoodName_1 = "pulses, other and products",
ID_2 = "23170.03",
FoodName_2 = "flour of pulses",
ID_3 = "23170.03.01",
FE2_3 = "",
FoodName_3 = "chick peas flour, dried, raw") %>%
add_row(
ID_0 = "FV",
FoodName_0 = "Fruits and Vegetables",
ID_1 = 2605,
FoodName_1 = "vegetables, other and products",
ID_2 = "1252",
FoodName_2 = "green garlic", #http://datalab.review.fao.org/datalab/caliper/web/concept-page/0406-garlic
ID_3 = "1252.01",
FE2_3 = "",
FoodName_3 = "garlic, fresh, raw") %>%
add_row(
ID_0 = "OT",
FoodName_0 = "Other foods",
ID_1 = 2645,
FoodName_1 = "spices, other and products",
ID_2 = "1699",
FoodName_2 = "other stimulant, spice and aromatic crops, n.e.c.",
ID_3 = "1699.08",
FE2_3 = "",
FoodName_3 = "moringa, leaves, raw") %>%
add_row(
ID_0 = "OT",
FoodName_0 = "Other foods",
ID_1 = 2645,
FoodName_1 = "spices, other and products",
ID_2 = "1699",
FoodName_2 = "other stimulant, spice and aromatic crops, n.e.c.",
ID_3 = "1699.09",
FE2_3 = "",
FoodName_3 = "hops, dried, raw")
### Cereals (CE) ----
#Fixing ID_1 codes
dictionary.df$ID_1[dictionary.df$ID_1 == "2806"] <- "2805"
#Fixing ID_2
dictionary.df$ID_2[dictionary.df$ID_2 == "23121.01"] <- "23120.01"
#Improving FoodName_3 description
dictionary.df$FoodName_3[dictionary.df$ID_3 == "23140.05.01"] <- "barley grain, pearl, dried, raw"
dictionary.df$FoodName_3[dictionary.df$ID_3 == "114.01"] <- "sorghum grain, average, dried, raw"
dictionary.df$FoodName_3[dictionary.df$ID_3 == "114.01"] <- "sorghum grain, average, dried, raw"
dictionary.df$FoodName_3[dictionary.df$ID_3 == "1193.04"] <- "fonio grain, white, refined, dried, raw"
dictionary.df$FoodName_3[dictionary.df$ID_3 == "118.03"] <- "millet grain, pearl, dried, raw"
#Adding other names
dictionary.df$Description2[dictionary.df$ID_3 == "23161.01.01"] <- "rice grain, white, polished, dried, raw"
dictionary.df$Description2[dictionary.df$ID_3 == "23161.02.01"] <- "rice grain, white, polished, dried, raw"
#├ New category from ID_2 ----
#Flour of sorghum, red
id2 <- "23120.06"
n1 <- dim(dictionary.df)[1]+1
n2 <- which(dictionary.df$ID_2 %in% id2)
dictionary.df[n1,] <- dictionary.df[n2,]
dictionary.df[n1,7] <- paste0(id2, ".01")
dictionary.df[n1,8] <- NA
dictionary.df[n1,9] <- "sorghum, red, flour raw"
dictionary.df[n1,13] <- "sorghum bicolor"
#Flour of millet, pearl
id2 <- "23120.05"
n1 <- dim(dictionary.df)[1]+1
n2 <- which(dictionary.df$ID_2 %in% id2)
dictionary.df[n1,] <- dictionary.df[n2,]
dictionary.df[n1,7] <- paste0(id2, ".01")
dictionary.df[n1,8] <- NA
dictionary.df[n1,9] <- "millet, pearl, flour, raw"
dictionary.df[n1,12] <- "also called bulrush millet"
dictionary.df[n1,13] <- "pennisetum glaucum"
#Rice, brown, raw
id2 <- "23162"
n1 <- dim(dictionary.df)[1]+1
n2 <- which(dictionary.df$ID_2 %in% id2)
dictionary.df[n1,] <- dictionary.df[n2,]
dictionary.df[n1,7] <- paste0(id2, ".01")
dictionary.df[n1,8] <- NA
dictionary.df[n1,9] <- "rice, brown, raw"
dictionary.df[n1,12] <- NA
dictionary.df[n1,13] <- "oryza sativa"
#├ New item (ID_3) ----
#Adding description
dictionary.df$Description1[dictionary.df$ID_3 == "23710.01"] <- "default pasta (spaghetti)"
#Flour of sorghum, white
n1 <- dim(dictionary.df)[1]+1
n2 <- which(dictionary.df$ID_3 == "23120.06.01")
dictionary.df[n1,] <- dictionary.df[n2,]
dictionary.df[n1,7] <- "23120.06.02"
dictionary.df[n1,8] <- NA
dictionary.df[n1,9] <- "sorghum, white, flour raw"
dictionary.df[n1,12] <- NA
dictionary.df[n1,13] <- "sorghum bicolor"
#Flour of millet, finger
n1 <- dim(dictionary.df)[1]+1
n2 <- which(dictionary.df$ID_3 == "23120.05.01")
dictionary.df[n1,] <- dictionary.df[n2,]
dictionary.df[n1,7] <- "23120.05.02"
dictionary.df[n1,8] <- NA
dictionary.df[n1,9] <- "millet, finger, flour,raw"
dictionary.df[n1,12] <- NA
dictionary.df[n1,13] <- "eleusine coracana"
#Macaroni
n1 <- dim(dictionary.df)[1]+1
n2 <- which(dictionary.df$ID_3 == "23710.01")
dictionary.df[n1,] <- dictionary.df[n2,]
dictionary.df[n1,7] <- "23710.02"
dictionary.df[n1,8] <- NA
dictionary.df[n1,9] <- "pasta, macaroni, wheat, dried, raw"
dictionary.df[n1,10] <- NA
dictionary.df[n1,11] <- NA
#rice, brown, boiled
id3 <- "23162.01"
n1 <- dim(dictionary.df)[1]+1
n2 <- which(dictionary.df$ID_3 %in% id3)
dictionary.df[n1,] <- dictionary.df[n2,]
dictionary.df[n1,7] <- paste0( str_extract(id3,
"[[:alnum:]]{2,5}\\.\\d{1,2}\\.\\d{1}|[[:alnum:]]{2,5}\\.\\d{1}"),
as.numeric(str_extract(id3, "[[:digit:]]$"))+1)
dictionary.df[n1,9] <- "rice, brown, boiled"
## ├├ rice, milled (husked) (23161.01) -----
food_desc <- c("rice, parboiled, imported, raw",
"rice, parboiled, imported, boiled",
"rice, white, imported, steamed")
other_name <- c(rep(NA, 3))
scientific_name <- c(rep("oryza sativa", 3))
taxon <- c(NA)
fex2_new <- c(NA)
# One input
id2 <- "23161.01"
desc1 <- c("Rice, Milled (Husked): White rice milled from imported husked rice. Includes semi-milled, whole-milled and parboiled rice.")
ref1 <- c("https://www.fao.org/faostat/en/#data/SCL")
taxon_ref <- c(NA)
# Function:
for(i in 1:length(food_desc)){
id2 <- id2
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new[i]
dictionary.df[n1,9] <- food_desc[i]
dictionary.df[n1,10] <- desc1
dictionary.df[n1,11] <- ref1
dictionary.df[n1,12] <- other_name[i]
dictionary.df[n1,13] <- scientific_name[i]
dictionary.df[n1,14] <- taxon[i]
dictionary.df[n1,15] <- taxon_ref
}
## ├├ rice, milled (23161.02) -----
food_desc <- c("rice grain, parboiled, local, dried, raw",
"rice grain, parboiled, local, dried, boiled",
"rice grain, red native, milled, local, dried, raw",
"rice grain, white, local, steamed",
"rice grain, white, local, boiled",
"rice grain, white, polished, local, dried, boiled")
other_name <- c(rep(NA, 2), "African rice, milled",
rep(NA, 3))
scientific_name <- c(rep("oryza sativa", 2),
"oryza glaberrima",
rep("oryza sativa", 3))
taxon <- c(NA)
fex2_new <- c(NA)
# One input
id2 <- "23161.02"
desc1 <- c("Rice, Milled: White rice milled from locally grown paddy. Includes semi-milled, whole-milled and parboiled rice.")
ref1 <- c("https://www.fao.org/faostat/en/#data/SCL")
taxon_ref <- c(NA)
# Function:
for(i in 1:length(food_desc)){
id2 <- id2
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new[i]
dictionary.df[n1,9] <- food_desc[i]
dictionary.df[n1,10] <- desc1
dictionary.df[n1,11] <- ref1
dictionary.df[n1,12] <- other_name[i]
dictionary.df[n1,13] <- scientific_name[i]
dictionary.df[n1,14] <- taxon[i]
dictionary.df[n1,15] <- taxon_ref
}
## ├├ Bread (F0020) -----
food_desc <- c("chapati, wheat flour, refined",
"chapati, wheat flour, unrefined",
"chapati, wheat flour, unrefined, with ghee",
"chapati, wheat flour, refined, with fortified ghee",
"bread, sweet, wheat flour, refined")
other_name <- c(rep(NA, 2),
"also called \"Indian chapati\"",
rep(NA, 2))
scientific_name <- c(rep(NA, 5))
taxon <- c(NA)
fex2_new <- c(NA)
#Manual inputs:
# One input
id2 <- "F0020"
desc1 <- c("A baked product of flour or meal of cereals, especially wheat. Includes ordinary, unleavened, crackers, rusks, etc (Unofficial definition)")
ref1 <- c("https://www.fao.org/faostat/en/#data/SCL")
taxon_ref <- c(NA)
# Function:
for(i in 1:length(food_desc)){
id2 <- id2
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new[i]
dictionary.df[n1,9] <- food_desc[i]
dictionary.df[n1,10] <- desc1
dictionary.df[n1,11] <- ref1
dictionary.df[n1,12] <- other_name[i]
dictionary.df[n1,13] <- scientific_name[i]
dictionary.df[n1,14] <- taxon[i]
dictionary.df[n1,15] <- taxon_ref
}
#Add - 23120.01 - bread, sweet, wheat flour, refined
#Manual inputs:
id2 <- "23120.01"
desc_new <- "cake, rice flour, fried"
fex2_new <- NA
scien_new <- NA
#Auto inputs:
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- which(dictionary.df$ID_3 %in% id3)
dictionary.df[n1,] <- dictionary.df[n2,]
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new
dictionary.df[n1,9] <- desc_new
dictionary.df[n1,13] <- scien_new
## ├├ other cereals n.e.c. (1199.9) -----
food_desc <- c("teff grain, dried, unrefined, raw",
"amaranth, grain, dried, raw" )
other_name <- c(rep(NA, 2))
scientific_name <- c("eragrostis tef",
"amaranthus spp.")
taxon <- c(NA)
fex2_new <- c(NA)
# One input
id2 <- "1199.9"
desc1 <- c("This subclass includes among all: Chenopodium pallidicaule (canagua or coaihua), Amaranthus caudatus (quihuicha or Inca wheat), Coix lacryma-jobi (adlay or Job's tears), Zizania aquatica (wild rice) and other cereal crops and hybrid grains that are not identified separately because of their minor relevance at the international level. (Unofficial definition)")
ref1 <- c("https://www.fao.org/faostat/en/#data/SCL")
taxon_ref <- c(NA)
# Function:
for(i in 1:length(food_desc)){
id2 <- id2
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new[i]
dictionary.df[n1,9] <- food_desc[i]
dictionary.df[n1,10] <- desc1
dictionary.df[n1,11] <- ref1
dictionary.df[n1,12] <- other_name[i]
dictionary.df[n1,13] <- scientific_name[i]
dictionary.df[n1,14] <- taxon[i]
dictionary.df[n1,15] <- taxon_ref
}
# 114 - Sorghum, Grain, Red, Dried, Raw
#Manual inputs:
id2 <- "114"
desc_new <- "sorghum grain, red, dried, raw"
fex2_new <- NA
scien_new <- "sorghum bicolor"
other_name <- NA
#Auto inputs:
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new
dictionary.df[n1,9] <- desc_new
dictionary.df[n1,12] <- other_name
dictionary.df[n1,13] <- scien_new
# 114 - Sorghum, Grain, white, Dried, Raw
#Manual inputs:
id2 <- "114"
desc_new <- "sorghum grain, white, dried, raw"
fex2_new <- NA
scien_new <- "sorghum bicolor"
other_name <- NA
#Auto inputs:
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new
dictionary.df[n1,9] <- desc_new
dictionary.df[n1,12] <- other_name
dictionary.df[n1,13] <- scien_new
# 23162 - Rice, brown, raw
#Manual inputs:
id2 <- "23162"
desc_new <- "rice grain, brown, dried, raw"
fex2_new <- NA
scien_new <- "oryza sativa"
other_name <- NA
#Auto inputs:
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new
dictionary.df[n1,9] <- desc_new
dictionary.df[n1,12] <- other_name
dictionary.df[n1,13] <- scien_new
# 23162 - Rice, red native, hulled, raw
#Manual inputs:
id2 <- "23162"
desc_new <- "rice grain, red native, brown, dried, raw"
fex2_new <- NA
scien_new <- "oryza glaberrima"
other_name <- "African rice, hulled"
#Auto inputs:
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new
dictionary.df[n1,9] <- desc_new
dictionary.df[n1,12] <- other_name
dictionary.df[n1,13] <- scien_new
## ├├ pastry (F0022) -----
food_desc <- c("dough, fried", "cake, plain butter", "cake, fruit",
"cake, sponge, homemade", "cake, sponge, without fat",
"cake, iced", "croissant, plain, unfortified",
"biscuit, savoury")
other_name <- c("mandazi (TZ06, KE18)", "cupcake", rep(NA,5))
scientific_name <- c(NA)
fex2_new <- c(NA)
id2 <- "F0022"
desc1 <- c(NA)
ref1 <- c(NA)
# Function:
for(i in 1:length(food_desc)){
id2 <- id2
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new
dictionary.df[n1,9] <- food_desc[i]
dictionary.df[n1,10] <- desc1
dictionary.df[n1,11] <- ref1[i]
dictionary.df[n1,12] <- other_name[i]
dictionary.df[n1,13] <- scientific_name[i]
}
# 23999.02 - Cheese straws/twists, retail
#Manual inputs:
id2 <- "23999.02"
desc_new <- "snacks, cheese flavour"
fex2_new <- NA
scien_new <- NA
other_name <- "Cheese straws/twists (UK21)"
#Auto inputs:
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new
dictionary.df[n1,9] <- desc_new
dictionary.df[n1,12] <- other_name
dictionary.df[n1,13] <- scien_new
# 23999.02 - Corn snacks
#Manual inputs:
id2 <- "23999.02"
desc_new <- "snacks, corn based"
fex2_new <- NA
scien_new <- NA
other_name <- NA
#Auto inputs:
id3 <- tail(sort(dictionary.df$ID_3[dictionary.df$ID_2 == id2]), n=1)
id3_new <-ifelse(is.na(id3)|id3 == "", paste0(id2, ".01"),
str_replace(id3, "[[:alnum:]]{1,3}$",
formatC(seq(from = str_extract(id3, "[[:digit:]]{1,3}$"), 99),
width=2, flag=0)[2]))
n1 <- dim(dictionary.df)[1]+1
n2 <- ifelse(is.na(id3)|id3 == "", which(dictionary.df$ID_2 %in% id2),
which(dictionary.df$ID_3 %in% id3))
#New entry - generation:
dictionary.df[n1,] <- dictionary.df[n2,]
#New entry - population:
dictionary.df[n1,7] <- id3_new
dictionary.df[n1,8] <- fex2_new
dictionary.df[n1,9] <- desc_new
dictionary.df[n1,12] <- other_name
dictionary.df[n1,13] <- scien_new
# 23999.02 - Potato snacks, pringle-type, fried in vegetable oil
#Manual inputs:
id2 <- "23999.02"
desc_new <- "snacks, potato based"