Suppose I have the following dataset:
library(data.table)
dt <- data.table(x = c(1, 2, 4, 5, 2, 3, 4))
> dt
x
1: 1
2: 2
3: 4
4: 5
5: 2
6: 3
7: 4
I would like to disconnect after the 4th row since when the first duplicate will happen (number 2).
Expected Result:
x
1: 1
2: 2
3: 4
4: 5
Needless to say, I'm not looking dt[1:4, ,][]because the real data set is more "complex."
I tried with shift(), .Ibut it did not work. One idea: dt[x %in% dt$x[1:(.I - 1)], .SD, ][].
source
share