# load Brazil 2017 data
f <- 'data/1-TRADE/CD/EXPORT/BRAZIL/DATAMYNE/DASHBOARD/2017/CD_BRAZIL_2017.csv'
obj <- get_object(object = f, bucket = 'trase-storage')
data <- read_delim(obj, delim = ";")
## Parsed with column specification:
## cols(
##   Date..Month. = col_character(),
##   Product.HS = col_double(),
##   HS.Description = col_character(),
##   Country.of.Destination = col_character(),
##   Port.of.Departure = col_character(),
##   FOB.Value..US.. = col_double(),
##   Exporter.Name = col_character(),
##   State...Department.of.the.Exporter = col_character(),
##   Exporter.Municipality = col_character(),
##   Transport.Method = col_character(),
##   Net.Weight = col_double(),
##   Exporter.CNJP = col_double(),
##   HS6 = col_double()
## )
## Warning: 1 parsing failure.
##   row           col expected      actual         file
## 10014 Exporter.CNJP a double SINFORMACAO <raw vector>
J <- list()
i = 1

comms <- c('beef', 'chicken', 'corn', 'cocoa', 'coffee', 'cotton', 'pork', 'leather', 'timber', 
           'palmoil', 'palmkernel', 'cpo', 'woodpulp', 'shrimps', 'soy', 'sugarcane')

# for each commodity in string of commodities
for (i in 1:length(comms)){    #)){
        

        # create dataframe, rows: HS codes, columns: weight
        numbers <- data.frame(commodity = commodities[[i]])
        
        # get leading zeroes
        numbers$commodity <- as.numeric(as.character(numbers$commodity))
        numbers$commodity <- AT.add.leading.zeros(numbers$commodity, digits = 6)
        
        # for each HS code, weight <- sum of weights in dataframe where hs code is that hs code (need new column for six digits)
        data$HS6 <- as.numeric(as.character(data$HS6))
        data$HS6 <- AT.add.leading.zeros(data$HS6, digits = 6)
        
        for (j in 1:length(numbers$commodity)){
                numbers$weight[j] <- sum(data$'Net.Weight'[data$HS6 %in% numbers$commodity[j]])
        }
        
        # total weight for that commodity <- sum of weights from table
        total_weight <- sum(numbers$weight)
        
        # new column: % <- weight column divided by total weight, and formatted *100 etc
        numbers$perc <- round((numbers$weight / total_weight) *100, digits = 4)
        
        # sort table by % column, descending
        numbers <- numbers[order(-numbers$perc),] 
        
        # ok, ready
        rownames(numbers) <- c()
        print(kable(numbers, caption = comms[i]))
        cat('\n')
        
}
beef
commodity weight perc
020230 1073020031 62.2618
050400 146128018 8.4790
020629 126210317 7.3233
020130 124723338 7.2370
010229 124706876 7.2361
160250 88106998 5.1124
020621 12897884 0.7484
020220 11962301 0.6941
021020 5496727 0.3189
010290 3959090 0.2297
020622 3712077 0.2154
010221 1646885 0.0956
020120 463730 0.0269
020610 367202 0.0213
010210 0 0.0000
020110 0 0.0000
020210 0 0.0000
chicken
commodity weight perc
020714 2699545048 68.3992
020712 1245581281 31.5597
010511 842101 0.0213
020713 783000 0.0198
010591 0 0.0000
010594 0 0.0000
020711 0 0.0000
020741 0 0.0000
160232 0 0.0000
corn
commodity weight perc
100590 29245735088 98.9709
110220 186931340 0.6326
110812 34349733 0.1162
151521 33893615 0.1147
100510 19995620 0.0677
110423 13433544 0.0455
110313 10170297 0.0344
230210 4655369 0.0158
151529 667998 0.0023
cocoa
commodity weight perc
180400 30480526 49.9800
180500 22361454 36.6669
180310 6996780 11.4729
180100 753767 1.2360
180200 194618 0.3191
180610 136297 0.2235
180320 62001 0.1017
coffee
commodity weight perc
090111 1647927506 94.8293
210111 84102663 4.8397
210112 3972694 0.2286
090121 1764584 0.1015
090122 10633 0.0006
090190 4362 0.0003
090112 0 0.0000
cotton
commodity weight perc
520100 833826831 94.9904
140420 21714203 2.4737
120729 16421003 1.8707
151229 3968644 0.4521
520291 1120622 0.1277
520299 478021 0.0545
230610 263222 0.0300
470610 6693 0.0008
520210 1255 0.0001
520300 1113 0.0001
120721 0 0.0000
151221 0 0.0000
pork
commodity weight perc
020329 561001823 83.7556
020649 70113052 10.4676
020322 21688615 3.2380
020321 9494724 1.4175
020641 4870789 0.7272
021019 1285954 0.1920
020630 377846 0.0564
020319 283762 0.0424
010310 258192 0.0385
010392 225901 0.0337
020311 206766 0.0309
021012 667 0.0001
010391 0 0.0000
020312 0 0.0000
021011 0 0.0000
leather
commodity weight perc
410411 242119503 53.2296
410419 127456932 28.0212
410712 47967678 10.5456
410441 17619402 3.8736
410792 9499849 2.0885
410150 2859694 0.6287
410711 2569612 0.5649
410791 1271399 0.2795
410449 964399 0.2120
410799 911932 0.2005
410719 836459 0.1839
410190 577395 0.1269
410120 204408 0.0449
timber
commodity weight perc
440711 1044764827 85.7109
440729 101245733 8.3060
440322 49050063 4.0240
440410 7540875 0.6186
440349 7143140 0.5860
440722 4426047 0.3631
440721 3270338 0.2683
440326 1088820 0.0893
440312 319650 0.0262
440420 53856 0.0044
440725 21616 0.0018
440311 15777 0.0013
440321 0 0.0000
440325 0 0.0000
440341 0 0.0000
440726 0 0.0000
440727 0 0.0000
440728 0 0.0000
palmoil
commodity weight perc
151110 88530349 98.4687
151190 1376775 1.5313
120710 0 0.0000
230660 0 0.0000
palmkernel
commodity weight perc
151329 2344946 88.9297
151321 291908 11.0703
cpo
commodity weight perc
151110 88530349 98.4687
151190 1376775 1.5313
woodpulp
commodity weight perc
470329 13007012322 93.9709
470200 642152998 4.6393
470321 192173823 1.3884
470100 184021 0.0013
470500 16000 0.0001
470311 4 0.0000
470319 0 0.0000
470411 0 0.0000
470419 0 0.0000
470421 0 0.0000
470429 0 0.0000
shrimps
commodity weight perc
030617 214476 100
030616 0 0
030635 0 0
030636 0 0
030695 0 0
soy
commodity weight perc
120190 68147704876 81.4448
230400 14176266088 16.9424
150710 1223792233 1.4626
150790 118575118 0.1417
120110 7103059 0.0085
120810 58596 0.0001
120100 0 0.0000
sugarcane
commodity weight perc
170114 23330603899 78.1951
170199 5363162110 17.9752
220710 1133524463 3.7991
170191 6780216 0.0227
220720 1617361 0.0054
170113 694876 0.0023
121292 0 0.0000
121293 129 0.0000
170111 0 0.0000
170310 0 0.0000