I would like to mix data.table pipelining with magrittr pipelining. I can go from data.table to %>% but I can't figure out how to get back to [][] data.table style pipelining.
Here's an example:
> tbl = data.table(grp=c(1,1,1,2,2,2,3,3,3,4,4), y=rnorm(11))
> tbl
grp y
1: 1 0.08150
2: 1 1.51330
3: 1 -0.26154
4: 2 -0.12746
5: 2 0.10747
6: 2 0.16502
7: 3 0.54139
8: 3 -0.04194
9: 3 0.02373
10: 4 2.00756
11: 4 1.05523
> tbl[, .(.N, mean(y)), by=grp][order(-N)] %>% head(n=3) %>% .[, N := NULL]
grp V2
1: 1 0.44442
2: 2 0.04834
3: 3 0.17439
> tbl[, .(.N, mean(y)), by=grp][order(-N)] %>% head(n=3) %>% .[, N := NULL][, plot(grp, V2)]
Error in `[.data.table`(., .[, `:=`(N, NULL)], , plot(grp, V2)) :
'by' or 'keyby' is supplied but not j
Calls: %>% ... freduce -> withVisible -> <Anonymous> -> [ -> [.data.table
>
How can I go back to [][] after %>% ?
I know that this particular example could be rewritten entirely with [] and no %>%, but I'm not interested in doing that every time. I'd like a way to be able to write [][] %>% [][] patterns.