You are still not telling us what list_all_sample is, but based on the information you are providing, I think that performing aggregation over your dataset would be better than using loops, see this example:
list_a <- list("Commodity Code" = c("0440000", "0451000", "0452000", "0459100", "0459200", "0459900", "0410000", "0422110"), "Commodity Name" = c("Corn", "Rye", "Oats", "Millet", "Sorghum", "Mixed Grains", "Wheat", "Rice"))
list_b <- list("Commodity Code" = c("2231000", "2223000", "2232000", "2221000", "2226000", "2222000", "2224000"), "Commodity Name" = c("Oilseed, Copra", "Oilseed, Cottonseed", "Oilseed, Palm Kernel", "Oilseed, Peanut", "Oilseed, Rapeseed", "Oilseed, Soybean", "Oilseed, Sunflowerseed"))
Sample_Data <- tibble::tribble(
~Commodity_Code, ~Commodity_Description, ~Country_Code, ~Country_Name, ~Market_Year, ~Calendar_Year, ~Month, ~Attribute_ID, ~Attribute_Description, ~Unit_ID, ~Unit_Description, ~Value,
"0440000", "Corn", "BL", "Bolivia", 1980, 2006, "07", "004", "Area Harvested", 4, "(1000 HA)", 293,
"0440000", "Corn", "BL", "Bolivia", 1980, 2006, "07", "020", "Beginning Stocks", 8, "(1000 MT)", 20,
"0440000", "Corn", "BR", "Brazil", 1980, 2006, "07", "004", "Area Harvested", 4, "(1000 HA)", 12810,
"0440000", "Corn", "BR", "Brazil", 1980, 2006, "07", "020", "Beginning Stocks", 8, "(1000 MT)", 1100,
"2223000", "Oilseed, Cottonseed", "BR", "Brazil", 1980, 2006, "06", "004", "Area Harvested", 4, "(1000 HA)", 2015,
"2223000", "Oilseed, Cottonseed", "BR", "Brazil", 1980, 2006, "06", "020", "Beginning Stocks", 8, "(1000 MT)", 0,
"2221000", "Oilseed, Peanut", "BR", "Brazil", 1980, 2006, "06", "004", "Area Harvested", 4, "(1000 HA)", 235,
"2221000", "Oilseed, Peanut", "BR", "Brazil", 1980, 2006, "06", "020", "Beginning Stocks", 8, "(1000 MT)", 0,
"0459200", "Sorghum", "BL", "Bolivia", 1980, 2006, "07", "004", "Area Harvested", 4, "(1000 HA)", 6,
"0459200", "Sorghum", "BL", "Bolivia", 1980, 2006, "07", "020", "Beginning Stocks", 8, "(1000 MT)", 0,
"0459200", "Sorghum", "BR", "Brazil", 1980, 2006, "07", "004", "Area Harvested", 4, "(1000 HA)", 92,
"0459200", "Sorghum", "BR", "Brazil", 1980, 2006, "07", "020", "Beginning Stocks", 8, "(1000 MT)", 0,
"Total Grains", "Total Grains", "BL", "Bolivia", 1980, 2006, "07", "004", "Area Harvested", 4, "(1000 HA)", 299,
"Total Grains", "Total Grains", "BR", "Brazil", 1980, 2006, "07", "004", "Area Harvested", 4, "(1000 HA)", 20,
"Total Grains", "Total Grains", "BL", "Bolivia", 1980, 2006, "07", "020", "Beginning Stocks", 8, "(1000 MT)", 12902,
"Total Grains", "Total Grains", "BR", "Brazil", 1980, 2006, "07", "020", "Beginning Stocks", 8, "(1000 MT)", 1100,
"Total Oilseeds", "Total Oilseeds", "BR", "Brazil", 1980, 2006, "06", "020", "Beginning Stocks", 8, "(1000 MT)", 2250,
"Total Oilseeds", "Total Oilseeds", "BR", "Brazil", 1980, 2006, "06", "004", "Area Harvested", 4, "(1000 HA)", 0
)
library(tidyverse)
Sample_Data %>%
mutate(Commodity_Code = case_when(Commodity_Code %in% list_a[[1]] ~ "Total Grains",
Commodity_Code %in% list_b[[1]] ~ "Total Oilseeds",
TRUE ~ Commodity_Code)) %>%
group_by(Commodity_Code,Country_Code,Country_Name,Market_Year,Calendar_Year,Month,Attribute_ID,Attribute_Description,Unit_ID,Unit_Description) %>%
summarise(Value = sum(Value))
#> # A tibble: 6 x 11
#> # Groups: Commodity_Code, Country_Code, Country_Name, Market_Year,
#> # Calendar_Year, Month, Attribute_ID, Attribute_Description, Unit_ID [6]
#> Commodity_Code Country_Code Country_Name Market_Year Calendar_Year Month
#> <chr> <chr> <chr> <dbl> <dbl> <chr>
#> 1 Total Grains BL Bolivia 1980 2006 07
#> 2 Total Grains BL Bolivia 1980 2006 07
#> 3 Total Grains BR Brazil 1980 2006 07
#> 4 Total Grains BR Brazil 1980 2006 07
#> 5 Total Oilseeds BR Brazil 1980 2006 06
#> 6 Total Oilseeds BR Brazil 1980 2006 06
#> # … with 5 more variables: Attribute_ID <chr>,
#> # Attribute_Description <chr>, Unit_ID <dbl>, Unit_Description <chr>,
#> # Value <dbl>
Created on 2019-08-09 by the reprex package (v0.3.0.9000)