I would like to set the first and the last value in a group to NA. Here is an example:
DT <- data.table(v = rnorm(12), class=rep(1:3, each=4))
DT[, v[c(1,.N)] := NA , by=class]
But this is not working. How can I do it?
At the moment, the way to go about this would be to first extract the indices, and then do one assignment by reference.
idx = DT[, .(idx = .I[c(1L, .N)]), by=class]$idx
DT[idx, v := NA]
I'll try and add this example to the Reference semantics vignette.
This may not be a one-liner, but it does have 'first' and 'last' in the code :)
> DT <- data.table(v = rnorm(12), class=rep(1:3, each=4))
> setkey(DT, class)
> classes = DT[, .(unique(class))]
> DT[classes, v := NA, mult='first']
> DT[classes, v := NA, mult='last']
> DT
v class
1: NA 1
2: -1.8191 1
3: -0.6355 1
4: NA 1
5: NA 2
6: -1.1771 2
7: -0.8125 2
8: NA 2
9: NA 3
10: 0.2357 3
11: 0.3416 3
12: NA 3
>
Order is also preserved for the non-key columns. I think that is a documented (committed to) feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With