0

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.

  • Have a peruse: https://www.datamentor.io/r-programming/if-else-statement/ – Andrew Bannerman Mar 27 '19 at 00:40
  • Where exactly do you mean? I tried to follow the if else ladder example on that website you linked but that gave me less results – Pej Haqshenas Mar 27 '19 at 01:03
  • Give some example data – Andrew Bannerman Mar 27 '19 at 02:25
  • 1
    Your syntax problem is that `=` is not a test---it is used for assignment. You need `==` to test equality. So, e.g., `orders$Market[i] = "US"` should be `orders$Market[i] == "US"`. You also are missing quotes around `LATAM`. However, you can simplify, instead of `x == a | x == b | x == c`, you can do `x %in% c(a, b, c)`, much nicer to read and write. – Gregor Thomas Mar 27 '19 at 03:28
  • Secondly, you can use the vectorized `ifelse()` instead of the one-at-a-time `if(){}else{}` inside a `for` loop. [This answer explains it fairly well](https://stackoverflow.com/a/18016872/903061) – Gregor Thomas Mar 27 '19 at 03:33
  • Also, while this might be a reasonable way to do this in other languages, it's a very roundabout pattern for R. Take a look at the `dplyr` package, specifically the `mutate` and `case_when` functions. It will make your code much clearer and easier to maintain. – divibisan Mar 27 '19 at 15:27

0 Answers0