I have a data that I want to first filter some rows and sum those remaining rows.
The filtering conditions as follows;
- for gr==1 find the last occurrence of
y_value==10
and keep the all rows before it (including the last occurrence of this value 10 row)! - for gr==2 find the first occurrence of
y_value==10
and keep all the rows after it (including the first occurrence of this value 10 row)!
The data is like this;
df <- data.frame(gr=rep(c(1,2),c(8,7)),
y_value=c(c(2,10,10,8,10,6,0,0),c(0,0,10,10,6,8,10)))
gr y_value
1 1 2
2 1 10
3 1 10
4 1 8
5 1 10
6 1 6
7 1 0
8 1 0
9 2 0
10 2 0
11 2 10
12 2 10
13 2 6
14 2 8
15 2 10
I tried this in the light of summing-rows-based-on-conditional-in-groups;
df_temp <- df %>%
group_by(gr) %>%
mutate(rows_to_aggregate=cumsum(y_value==10)) %>%
filter(ifelse(gr==1, rows_to_aggregate !=0, ifelse(gr==2, rows_to_aggregate ==0 | y_value==10, rows_to_aggregate ==0))) %>%
filter(ifelse(gr==1, row_number(gr) != 1, ifelse(gr==2, row_number(gr) != n(), rows_to_aggregate ==0)))
but the if I do rows_to_aggregate !=0
in gr==1
the rows in the interest will be gone! Any guide at this point will be appreciated!