I have a table like the following:
date X1 X2 X3
1/1 0 3 34
2/1 0 7 65
3/1 0 13 0
4/1 25 4 65
5/1 35 0 0
6/1 4 6 9
7/1 0 0 0
How can I use dplyr
to select rows only after all Xs start appearing non-zero values. In that case, select only rows for dates 4/1
, 5/1
, 6/1
and 7/1
. Thank you.
df %>%
filter(cumsum(if_all(starts_with('X'), ~.x > 0))>0)
date X1 X2 X3
1 4/1 25 4 65
2 5/1 35 0 0
3 6/1 4 6 9
4 7/1 0 0 0
or even:
df %>%
filter(cumsum(if_all(starts_with('X'))>0)>0)
date X1 X2 X3
1 4/1 25 4 65
2 5/1 35 0 0
3 6/1 4 6 9
4 7/1 0 0 0