I have a dataframe that looks like this:
df <- data.frame(id1 = c(232, 232, 232,233, 233, 233), value = c(20.1, -11.1, 11.1, 3.1, -4.1, 4.1),
dt = c('11-20', '11-02', '11-02', '11-03', '11-04','11-04'), id2 = c(21, 22, 23, 21, 25 ,26))
I want to
- group by id1
- create a new logical column 'flag'
- flag should have TRUE
- when id1 have same dt
- same value but sign reversed and
- different id2
the expected output will look like this
id1 value dt id2 flag
1 232 20.1 11-20 21 FALSE
2 232 -11.1 11-02 22 TRUE
3 232 11.1 11-02 23 TRUE
4 233 3.1 11-03 21 FALSE
5 233 -4.1 11-04 25 TRUE
6 233 4.1 11-04 26 TRUE