微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

IFSUMPRODUCT使用R的MAX公式

如何解决IFSUMPRODUCT使用R的MAX公式

我在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","Crystal","Henry"),`unit price` = c(2,1,4,5,2,3,7,6),item = c("x","x","y","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",item = structure(list(),"collector"))),default = structure(list(),class = c("collector_guess",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   

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。