Throw away first and last n rows
data.table, r
Solution
In this case you know the name of one column (`row1`) that exists, so using `length(<any column>)` returns the number of rows within the unnamed temporary `data.table`:
example=data.table(row1=seq(1,1000,1),row2=seq(2, 3000,3))
e2=example[row1%%2==0]
ans1 = e2[100:(nrow(e2)-100)]
ans2 = example[row1%%2==0][100:(length(row1)-100)]
identical(ans1,ans2)
[1] TRUE
Problem
I have a `data.table` in R where I want to throw away the first and the last n rows. I want to to apply some filtering before and then truncate the results. I know I can do this this way: ``` example=data.table(row1=seq(1,1000,1),row2=seq(2, 3000,3)) e2=example[row1%%2==0] e2[100:(nrow(e2)-100)] ``` Is there a possiblity of doing this in one line? I thought of something like: ``` example[row1%%2==0][100:-100] ``` This of course does not work, but is there a simpler solution which does not require a additional variable?