I have a dataset "dat" as follows:
ChromKey CHROM POS ID REF ALT length
11438 1 chr1 27023450 <NA> AGCG A 4
11755 1 chr1 27023767 <NA> CA C 3
12521 1 chr1 27057930 <NA> GA G 2
13174 1 chr1 27088681 <NA> TC T 3
14861 1 chr1 27100181 <NA> CGCA C 2
15593 1 chr1 27101426 <NA> TCTAA T 5
This dataset was created as a subset of another much larger dataset that contains each of the rows in dat and more. Let's call this original full dataset "dat.ori". (The numbers on the extreme left are the row numbers from the dat.ori dataset that have been subsetted to create dat).
From the original larger dataset I would like to create a dataset such that I can extract the rows that are in dat along with n rows above and below that row number, where n is the value given under the variable length in dat. For example, the rows I need extracted from dat.ori are
11434, 11435, 11436, 11437, 11438, 11439, 11440, 11441, 11442, 11752, 11753,
11754, 11755, 11756, 11757, 11758 and so on
That is 4 rows above and below 11438, 3 rows above and below 11755, 2 rows above and below 12521 etc.
Is there a way to do this in R? Many thanks! :)
(Apologies, its not the most reproducible example but I will try and edit this so that respondents can reproduce the example)
UPDATE: Here's what I did (from: Returning above and below rows of specific rows in r dataframe)
myRows=c(rownames(dat))
rowRanges <- lapply(which(rownames(dat.ori) %in% myRows), function(x) x + c(-1:1))
final=lapply(rowRanges, function(x) dat.ori[x, ])
This gives me exactly what I need but it gives me just one row above and below (set by c(-1:1)). What I need is this to be tweaked so that I get n rows above and below where n is determined by dat$length