0
votes

New R user here trying to do a conditional lookup between two dataframes in R and it gives me an error.

I was wondering if someone could help me understand the error and the solution to go around it.

The data:

Df1: Matches

structure(list(Locale = c("JPN", "KOR", "AUS", "KOR", "KOR", "CHN", "SAU", "USA", "GBR", "TWN", "CHN", "GBR", "CAN", "CAN", "FRA", "KOR", "GBR", "USA", "USA", "KOR", "HKG", "TWN", "KOR", "USA", "KOR", "KOR", "SAU", "SAU", "HKG", "GBR", "CAN", "JPN", "USA", "HKG", "KOR", "AUS", "KOR", "GBR", "USA", "CAN", "SAU", "HKG", "CAN", "JPN", "CHN", "AUS", "USA", "CAN", "TWN", "KOR", "JPN", "KOR", "CAN", "USA", "FRA", "GBR", "GBR", "TWN", "JPN", "FRA", "KOR", "TWN", "AUS", "USA", "TWN", "HKG", "CAN", "AUS", "GBR", "KOR", "GBR", "AUS", "FRA", "FRA", "SAU", "TWN", "CHN", "AUS", "GBR", "AUS", "USA", "HKG", "GBR", "CAN", "AUS", "USA", "USA", "CAN", "HKG", "TWN", "FRA", "GBR", "CHN", "USA", "GBR", "CAN", "JPN", "CHN", "JPN", "HKG"), Competitor Display Price = c(25597.0546, 706957.2788, 575, 905000, 1452611.0261, 6493.6353, 439.4715, 955, 315, 229.9353, 466.83, 435.7947, 335.1642, 166.0945, 135, 172, 395, 590, 245, 307, 4879.2187, 554.6057, 578512.5094, 191.7, 336139.422, 731606, 1093.1852, 196.1544, 29900, 1210, 703.3008, 46671.9972, 750, 2896.449, 3750.33, 495.7828, 455.77, 170, 85, 303.47, 108.1748, 2399.92, 64.0402, 87.95, 608.33, 2580, 246.771, 927.99, 676.6716, 455.85, 104753.0098, 372082.6828, 1270.3944, 141.0007, 1295, 339.6662, 245.16, 806.8085, 29160, 140, 340000, 591.609, 576.6296, 160.6172, 1597, 862.7353, 584.0043, 300.5162, 690, 729.64, 180, 387.2474, 1070, 191.9559, 883, 449, 248, 114.0814, 491, 1668.0422, 1200, 10830, 71.9895, 467, 1878.1158, 695, 2073.9102, 885, 2772.32, 421.144, 70, 170, 2388.0271, 602.1213, 350, 550, 325.67, 807.77, 25450.9455, 5999.788)), .Names = c("Locale", "Competitor Display Price"), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"))

Df2: Shipping

structure(list(Locale = c("USA", "CAN", "GBR", "JPN", "FRA", "CHN", "AUS", "KOR", "HKG", "TWN", "SAU"), Currency = c("USD", "CAD", "GBP", "JPY", "EUR", "USD", "AUD", "USD", "HKD", "USD", "USD"), Limit = c(0, 0, 200, 35000, 250, 300, 450, 350, 2500, 350, 500), Cost = c(0, 0, 15, 2750, 20, 25, 30, 25, 200, 25, 40)), .Names = c("Locale", "Currency", "Limit", "Cost"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -11L))

The code:

 Matches<-Matches %>%
  mutate(Shipping=mapply(function(x, y)sum(Shipping[Shipping$Locale == x & Shipping$Limit > y , "Cost"]),
                        Locale,`Competitor Display Price`))

What I would like to do : For each locale, if the Competitor display price in matches is below the limit in in shipping then mutate the cost from shipping to matches, else put zero.

Thanks a lot for your help.

1

1 Answers

0
votes

Instead of using a mapply, I used a 2 step approach with merge.