I have a data table (dt) that has a datetime column. I also have a separate list of datetimes (L).
How can I return a subset of dt that has +/- N rows for each match of an item in L against the datetime column of dt?
I am interested in doing this generically as possible, so this case is a datetime, but I would also be interested in doing it for factors and integers as well. Ideally this would dedup any overlaps as well.
Basically this would be something like grep 'foo|foo1' -A10 -B10 which translates to “Show lines that match foo or foo1 including 10 lines before and after”.
so as an example
then the row numbers of your matches are
Now say you want from 3 rows before to 1 after:
Now
outeris your friend:is almost what you’re looking for, we just
need to make sure the entries are all valid row numbers (there could be negative numbers in
rows):rows <- rows [rows %in% seq_len (nrow (dt))]you may also want to get rid of duplicates:
rows <- unique (rows)if you want to preserve the matrix structure of
rows, maybe setting invalid indices toNAis better than removing them:rows [! rows %in% seq_len (nrow (dt))] <- NAnow you return