IF(SUMPRODUCT(使用R的MAX公式

时间:2020-11-08 03:29:33

标签: r if-statement excel-formula max sumproduct

我在R中具有下表,并希望产生一个附加列,该列在Excel中将使用以下公式执行:

= IF(B2 = 0,SUMPRODUCT(MAX(($ A $ 2:$ A $ 11 = A2)($ C $ 2:$ C $ 11 = C2)($ B $ 2:$ B $ 11))),B2)

此公式表示: 如果单价= 0,则: 将所有其他销售的最高单价返回 那个顾客 相同的项目。

如果单价不等于零,则返回相同的单价。

关于A:C列的期望输出是:

  • 单价2
  • 2
  • 5
  • 1
  • 2
  • 4
  • 5
  • 2
  • 3
  • 7
  • 6
structure(list(customer = c("John", "Atticus", "Sally", "Bridget", 
"John", "Atticus", "Bridget", "Atticus", "Crystal", "Henry"), 
    `unit price` = c(2, 0, 1, 0, 4, 5, 2, 3, 7, 6), item = c("x", 
    "x", "y", "y", "y", "x", "y", "x", "x", "x")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), spec = structure(list(
    cols = list(customer = structure(list(), class = c("collector_character", 
    "collector")), `unit price` = structure(list(), class = c("collector_double", 
    "collector")), item = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

# A tibble: 10 x 3
   customer `unit price` item 
   <chr>           <dbl> <chr>
 1 John                2 x    
 2 Atticus             0 x    
 3 Sally               1 y    
 4 Bridget             0 y    
 5 John                4 y    
 6 Atticus             5 x    
 7 Bridget             2 y    
 8 Atticus             3 x    
 9 Crystal             7 x    
10 Henry               6 x   

1 个答案:

答案 0 :(得分:0)

使用group_by来考虑每个客户的计算,然后使用mutate添加列:

library(dplyr)
DF %>% 
  group_by(customer) %>%
  mutate(unit_price2 = if_else(`unit price` == 0, max(`unit price`), `unit price`))