Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: data.table, set first and last value of a group to NA

Tags:

r

data.table

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?

like image 708
NoBackingDown Avatar asked Mar 05 '15 11:03

NoBackingDown


2 Answers

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.

like image 125
Arun Avatar answered Sep 17 '22 00:09

Arun


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.

like image 20
Clayton Stanley Avatar answered Sep 21 '22 00:09

Clayton Stanley