`case_when`函数中`~`后面的条件项
我想在~incase_when函数之后放置一个条件项。我的例子:
df:
df <- structure(list(x = c("a", "a", "a", "b", "b", "b", "c", "c",
"c", "a", "a", "a"), y = 1:12), class = "data.frame", row.names = c(NA,
-12L))
不工作的代码:
library(dplyr)
df %>%
group_by(x) %>%
mutate(y = case_when(x=="b" ~ cumsum(y),
TRUE ~ y)) %>%
mutate(y = case_when(x=="a" ~ "what I want: last value of group "b" in column y",
TRUE ~ y))
用一句话来说:
group_byx- 计算列中的
cumsum组by - 取该组 (=b) 的最后一个值 (=15) 和
- 将此值 (=15) 放入
ygroup 所在的列a
所需的输出:
x y
<chr> <dbl>
1 a 15
2 a 15
3 a 15
4 b 4
5 b 9
6 b 15
7 c 7
8 c 8
9 c 9
10 a 15
11 a 15
12 a 15
非常感谢!!!
回答
在这种情况下,group_by()没有必要(虽然它有助于可读性等):
df %>%
mutate(y = case_when(x == "b" ~ cumsum(y * (x == "b")),
x == "a" ~ max(cumsum(y[x == "b"])),
TRUE ~ y))
x y
1 a 15
2 a 15
3 a 15
4 b 4
5 b 9
6 b 15
7 c 7
8 c 8
9 c 9
10 a 15
11 a 15
12 a 15
回答
只需添加ungroup()您计算2日前mutate和使用last与条件得到最后y用x == "b"
library(dplyr)
df %>%
group_by(x) %>%
mutate(y = case_when(x=="b" ~ cumsum(y),
TRUE ~ y)) %>%
# add the ungroup here
ungroup() %>%
# and then the value is like this
mutate(y = case_when(x=="a" ~ last(y[x == "b"]),
TRUE ~ y))
#> # A tibble: 12 x 2
#> x y
#> <chr> <int>
#> 1 a 15
#> 2 a 15
#> 3 a 15
#> 4 b 4
#> 5 b 9
#> 6 b 15
#> 7 c 7
#> 8 c 8
#> 9 c 9
#> 10 a 15
#> 11 a 15
#> 12 a 15
由reprex 包( v2.0.0 )于 2021 年 4 月 22 日创建