I am trying to complete a problem and I believe I'm running into a sort of formatting error for my if statements? My code is partially working in the sense that it is giving a sort of shipping surcharge, however, not correctly to which market I'm asking of it.
The question at hand is asking me to perform this:
In the imported data frame, create another column named “shipping_surcharge” whose value is computed based on the Region and Sales as follows.
a. If the Region Market is US, Canada or LATAM and Sales is less than $200, the shipping surcharge is 10% of Sales. For these regions markets, if Sales is $200 or more, the shipping surcharge is 15% of Sales.
b. If the Region Market is EMEA, EU or Africa and Sales is less than $250, the shipping surcharge is 15% of Sales. For these regions markets, if Sales if $250 or more, the shipping surcharge is 25% of Sales.
c. For the APAC region market, if Sales is less than $150, the shipping surcharge is 20% of Sales. Otherwise, it is 30% of Sales.
The code I have written thus far is this:
orders$shipping_surcharge <- ""
for(i in (1:n))
{
if(orders$Market[i] = "US" | orders$Market[i] = "Canada" | orders$Market[i] = LATAM & orders$Sales[i] < 200)
{
orders$shipping_surcharge[i] <- (0.10 * orders$Sales)
}
else if(orders$Sales[i] >= 200)
{
orders$shipping_surcharge[i] <- (0.15 * orders$Sales)
}
else if(orders$Market[i] = "EMEA" | orders$Market[i] = "EU" | orders$Market[i] = "Africa" & orders$Sales < 250)
{
orders$shipping_surcharge[i] <- (0.15 * orders$Sales)
}
else if(orders$Sales[i] >= 250)
{
orders$shipping_surcharge[i] <- (0.25 * orders$Sales)
}
else if(orders$Market[i] = "APAC" & orders$Sales[i] < 150)
{
orders$shipping_surcharge[i] <- (0.20 * orders$Sales)
}
else orders$shipping_surcharge[i] <- (0.30 * orders$Sales)
}
If you could explain to me what is wrong with my syntax so that I can understand in the future if I'm ever tested on it. Thank you in advance.