mapply函数出错(评估错误:仅在具有所有数字变量的数据框上定义)

时间:2018-01-22 22:39:02

标签: function mapply mutate

新R用户在这里试图在R中的两个数据帧之间进行条件查找,这给了我一个错误。

我想知道是否有人可以帮助我理解错误和解决方案。

数据:

Df1:匹配

  

结构(列表(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)),. Name = c(“Locale”,“竞争对手显示价格”),row.names = c(NA,-100L),class =   c(“tbl_df”,“tbl”,“data.frame”))

Df2:送货

  

结构(列表(Locale = c(“USA”,“CAN”,“GBR”,“JPN”,“FRA”,“CHN”,   “AUS”,“KOR”,“HKG”,“TWN”,“SAU”),货币= c(“美元”,“加元”,“英镑”,   “JPY”,“EUR”,“USD”,“AUD”,“USD”,“HKD”,“USD”,“USD”),Limit = c(0,   0,200,35000,250,300,450,350,2500,350,500),成本= c(0,0,   15,2750,20,25,30,25,200,25,40)),. Name = c(“Locale”,   “货币”,“限制”,“成本”),类= c(“tbl_df”,“tbl”,   “data.frame”),row.names = c(NA,-11L))

代码:

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

我想做什么: 对于每个区域设置,如果匹配中的竞争对手显示价格低于发货中的限制,则将成本从发货改为匹配,否则将零。

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我没有使用mapply,而是使用了合并的两步法。