1

I want to select rows that appear after first V (in action column) for each user.

df<-read.table(text="
user   action
1        D
1        D
1        P
1        E
1        V
1        D
1        D
2        E
2        V
2        V
2        P",header=T,stringsAsFactors = F)

resutl:
user   action
1       V
1       D
1       D
2       V
2       V
2       P
Cina
  • 9,759
  • 4
  • 20
  • 36

1 Answers1

5

Using cumsum in a group_by + filter you could do:

library(dplyr)

df |> 
  group_by(user) |> 
  filter(cumsum(action == "V") >= 1) |> 
  ungroup()
#> # A tibble: 6 × 2
#>    user action
#>   <int> <chr> 
#> 1     1 V     
#> 2     1 D     
#> 3     1 D     
#> 4     2 V     
#> 5     2 V     
#> 6     2 P

And thanks to the comment by @r2evans this could be simplified by using cumany:

df |> 
  group_by(user) |> 
  filter(cumany(action == "V")) |> 
  ungroup()
#> # A tibble: 6 × 2
#>    user action
#>   <int> <chr> 
#> 1     1 V     
#> 2     1 D     
#> 3     1 D     
#> 4     2 V     
#> 5     2 V     
#> 6     2 P
stefan
  • 90,330
  • 6
  • 25
  • 51
  • 4
    code golf: `filter(cumany(action == "V"))` in your pipe – r2evans Dec 15 '22 at 19:47
  • @r2evans. You should post this as an answer. – TarJae Dec 15 '22 at 19:55
  • 1
    Thanks @TarJae, but I don't think the replacement offers any innovation over stefan's answer: it uses the same premise, and I've occasionally posted non-dplyr answers that inadvertently relied on `cumany`/`cumall`, so I've found that `cumsum(.)>0` is a great macro-replacement for `cumany`. – r2evans Dec 15 '22 at 19:57
  • 1
    lol thanks was on the wrong way with the window function. just removed. – TarJae Dec 15 '22 at 19:58
  • 1
    Thx @r2evans for - once more - teaching me something new. – stefan Dec 15 '22 at 20:00
  • thax, just out of curiosity: what is difference between `%>%` and `|>` ? – Cina Dec 15 '22 at 20:15
  • 1
    `|>` is the native pipe introduced with R 4.1.0. For more on the differences compared to the `magrittr` `%>%` see https://stackoverflow.com/questions/67633022/what-are-the-differences-between-rs-new-native-pipe-and-the-magrittr-pipe – stefan Dec 15 '22 at 20:19
  • 1
    One additional remark to keep in mind: In general you could as in my code replace `|>` by `%>%`, while unfortunately it does not work always in the reverse direction. – stefan Dec 15 '22 at 20:27