Reputation: 109
Seemingly simple question yet I can't figure it out nor find the answer. I have the following data:
tibble(A = c(0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0),
B = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2))
I now wish to mark/flag (Not extract) the last non-zero value(or the last time A == 1
) in a separate column, per group B
.
Ideally I would like to do so using dplyr:
DF %>% group_by(B) %>%
mutate(C = ???)
I've found lots of answers on how to extract this value, but none on how to flag it. The closest I found was last(A[A==1])
, but this sets all C values equally. Instead I only want the last non-zero value per group marked.
My expected output would be something like:
tibble(A = c(0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0),
B = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2),
C = c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0))
Upvotes: 0
Views: 215
Reputation: 11584
Does this work:
library(dplyr)
df %>% group_by(B) %>% mutate(C = case_when(row_number() == max(which(A == 1)) ~ 1L, TRUE ~ 0L))
# A tibble: 14 x 3
# Groups: B [2]
A B C
<dbl> <dbl> <int>
1 0 1 0
2 0 1 0
3 0 1 0
4 0 1 0
5 1 1 0
6 0 1 0
7 1 1 1
8 0 2 0
9 1 2 0
10 1 2 0
11 0 2 0
12 0 2 0
13 1 2 1
14 0 2 0
Upvotes: 3