IF(SUMPRODUCT(MAX 公式使用 R
IF(SUMPRODUCT(MAX formula using R
我在 R 中有以下 table,并希望生成一个额外的列,在 Excel 中,将使用以下公式执行:
=IF(B2=0,SUMPRODUCT(MAX(($A$2:$A$11=A2)($C$2:$C$11=C2)($ B$2:$B$11))),B2)
这个公式表示:
如果单价 = 0,则:
return 所有其他销售的最高单价
同一个客户和
相同的项目。
如果单价不为零,则return相同的单价。
关于列 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
使用group_by
在per-customer的基础上考虑计算,然后mutate
添加列:
library(dplyr)
DF %>%
group_by(customer) %>%
mutate(unit_price2 = if_else(`unit price` == 0, max(`unit price`), `unit price`))
我在 R 中有以下 table,并希望生成一个额外的列,在 Excel 中,将使用以下公式执行:
=IF(B2=0,SUMPRODUCT(MAX(($A$2:$A$11=A2)($C$2:$C$11=C2)($ B$2:$B$11))),B2)
这个公式表示: 如果单价 = 0,则: return 所有其他销售的最高单价 同一个客户和 相同的项目。
如果单价不为零,则return相同的单价。
关于列 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
使用group_by
在per-customer的基础上考虑计算,然后mutate
添加列:
library(dplyr)
DF %>%
group_by(customer) %>%
mutate(unit_price2 = if_else(`unit price` == 0, max(`unit price`), `unit price`))