`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))

用一句话来说:

  1. group_by x
  2. 计算列中的cumsumby
  3. 取该组 (=b) 的最后一个值 (=15) 和
  4. 将此值 (=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与条件得到最后yx == "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 日创建


以上是`case_when`函数中`~`后面的条件项的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>