4

I have a data frame with certain values in a Mark column. I want to extract n values before and after a mark occurs (including the row with the mark).

I find the values I need by using indices <- which(df$Mark == 1) where 1 is the value I'm looking for. Now I need for example the indices of 5 rows before that and 5 rows after (and the one with the mark, so 11 rows in total).

I was thinking about looping through the indices to increase and decrease it by n, and then to subset the data frame with the appended indices. It would be quite nasty though.

Is there a faster way to do it? e.g. in dplyr? Base R answer will be fine as well.

PS: I read the similar question but it doesn't seem to fit my problem.

Here's the df:

df <- structure(list(CH1 = c(-0.02838132, -0.02642141, -0.02511601, 
-0.02443906, -0.02414024, -0.02417388, -0.02451562, -0.02393946, 
-0.02242496, -0.02104852, -0.0198534, -0.018965, -0.01853905, 
-0.01837877, -0.01857743, -0.01847437, -0.0176798, -0.01672419, 
-0.01594565, -0.01522826, -0.01485198, -0.01484227, -0.01507997, 
-0.01556828, -0.01534458, -0.01473233, -0.01376753, -0.01251296, 
-0.0116294, -0.01064516, -0.00966026, -0.00970934, -0.00969434, 
-0.00921217, -0.00881855, -0.00832315, -0.00793322, -0.00718289, 
-0.00643288, -0.00574532, -0.00535603, -0.00503564, -0.00469125, 
-0.00449608, -0.00426023, -0.00406978, -0.00401041, -0.00293273, 
-0.00154294, -0.0012401, -0.00108466, -0.00116468, -0.00121755, 
-0.00127168, -0.00099938, 0.00017319, 0.0019737, 0.00333815, 
0.00396771, 0.00439491, 0.00482015, 0.00515174, 0.0054591, 0.00657748, 
0.00863549, 0.01048496, 0.01175601, 0.01272887, 0.01350854, 0.0140988, 
0.01475749, 0.01568579, 0.0178412, 0.02036553, 0.02206326, 0.02315541, 
0.0241971, 0.02509713, 0.02599812, 0.02695202, 0.02829221, 0.03048931, 
0.03233365, 0.03385062, 0.03544046, 0.03690707, 0.03846173, 0.03980747, 
0.04145224, 0.04344824, 0.04491818, 0.04621653, 0.04728952, 0.04851875, 
0.04968494, 0.05085734, 0.05207405, 0.05288386, 0.05377864, 0.05486108, 
0.05593761, 0.0570737, 0.05811917, 0.0593426, 0.06005302, 0.05993605, 
0.05984828, 0.06032347, 0.06089914, 0.06177185, 0.06246712, 0.06323557, 
0.06413276, 0.06416812, 0.06303713, 0.06264461, 0.06301019, 0.06348586, 
0.06426832, 0.06509175, 0.06570335, 0.06598329, 0.06489886, 0.06344099, 
0.06281661, 0.06292738, 0.0630922, 0.06334323, 0.06376194, 0.0640305, 
0.06399924, 0.06292669, 0.06141425, 0.06046086, 0.06002845, 0.05977921, 
0.05952547, 0.05947563, 0.05888767, 0.05753626, 0.05571093, 0.05391346, 
0.053135, 0.05240138, 0.05196891, 0.05157123, 0.05107314, 0.05004111, 
0.04812315, 0.04601065, 0.04457145, 0.04376672, 0.04318091, 0.04265054, 
0.04222059, 0.041618, 0.0403326, 0.03810122, 0.03623468, 0.03515417, 
0.0343935, 0.03381848, 0.03330182, 0.03288956, 0.03268627, 0.03136984, 
0.02941283, 0.02847409, 0.02766387, 0.0268678, 0.02645577, 0.02606292, 
0.02592612, 0.0258327, 0.02477442, 0.02381663, 0.02342893, 0.02307516, 
0.02289283, 0.02281655, 0.02268435, 0.02245292, 0.02224212, 0.02203094, 
0.02189966, 0.02157357, 0.02129673, 0.02102508, 0.02140636, 0.02188274, 
0.02238155, 0.02332248, 0.02454547, 0.02617604, 0.0281874, 0.03046315, 
0.03274331, 0.03508138, 0.03754183, 0.04001183, 0.04252412, 0.04485972, 
0.04726444, 0.04945699, 0.05171933, 0.05405642, 0.05621058, 0.05858717, 
0.06119974, 0.0631874, 0.06494498, 0.06654966, 0.06778654, 0.06895418, 
0.0702159, 0.07208018, 0.07471886, 0.07640609, 0.07795521, 0.07929013, 
0.08029186, 0.08135373, 0.08218034, 0.08313267, 0.08513113, 0.08683419, 
0.08791834, 0.08894015, 0.08975692, 0.09043255, 0.09113128, 0.09186111, 
0.09291916, 0.09414985, 0.09492029, 0.09583852, 0.09664483, 0.09738685, 
0.09791321, 0.09827693, 0.09842386, 0.09819575, 0.09783525, 0.09711579, 
0.09588714, 0.09464117, 0.09342161, 0.09221725, 0.09094498, 0.08979087, 
0.08813678, 0.08722136, 0.08660734, 0.0863056, 0.08614786, 0.08576027, 
0.08508192, 0.08408207, 0.08224716, 0.0805236, 0.0793857, 0.07835744, 
0.07776693, 0.07704602, 0.0762578, 0.0748622, 0.07237066, 0.06983608, 
0.06798425, 0.06677078, 0.0660528, 0.06569698, 0.06521391, 0.06434717, 
0.06249718, 0.06009818, 0.05800739, 0.05674874, 0.05583431, 0.05525231, 
0.05479279, 0.05451269, 0.05392969, 0.05218898, 0.05015828, 0.04889652, 
0.04834132, 0.04789649, 0.04757991, 0.04729923, 0.04713846, 0.04664839, 
0.044963, 0.0434754, 0.04290805, 0.04229798, 0.04186826, 0.04133299, 
0.04069157, 0.03980917, 0.03850414, 0.03609292, 0.03422226, 0.03281199, 
0.03131085, 0.03030436, 0.02957696, 0.02881902, 0.02801267, 0.0266918, 
0.02524513, 0.02468021, 0.02422629, 0.02412119, 0.02414609, 0.02431383, 
0.02445115, 0.02420395, 0.02307613, 0.0225228, 0.02239294, 0.02228146, 
0.02247078, 0.02297619, 0.02339916, 0.02380192, 0.02367893, 0.02331219, 
0.02357285, 0.0239001, 0.02413282, 0.02442478, 0.02460252, 0.02484779, 
0.02539408, 0.02547098, 0.02568989, 0.02612677, 0.02653343, 0.02691505, 
0.02732947, 0.02783551, 0.02845577, 0.0294369, 0.03000503, 0.0303594, 
0.03106044, 0.03183592, 0.03254643, 0.03336877, 0.03433665, 0.03611183, 
0.03759354, 0.03864425, 0.03966344, 0.04067133, 0.04175726, 0.04283931, 
0.04391302, 0.04588513, 0.04825597, 0.04982677, 0.05137081, 0.05256286, 
0.05363528, 0.05468207, 0.05576433, 0.05764562, 0.06039843, 0.06209074, 
0.06330606, 0.06437107, 0.06532845, 0.06612719, 0.06689882, 0.06780636, 
0.06962782, 0.07139035, 0.07266567, 0.07378628, 0.07471222, 0.07541681, 
0.07637413, 0.07729325, 0.07797043, 0.07928976, 0.08020929, 0.08104116, 
0.08185486, 0.08268223, 0.08352671, 0.08418175, 0.08467345, 0.0845037, 
0.08452599, 0.08504328, 0.08524517, 0.08562133, 0.08602719, 0.08630189, 
0.08619381, 0.08511638, 0.08378159, 0.08298928, 0.08275849, 0.08255187, 
0.08253576, 0.08248511, 0.08237054, 0.08131169, 0.07927644, 0.07758952, 
0.07666323, 0.07611373, 0.07583219, 0.07563592, 0.07526416, 0.07413918, 
0.07218219, 0.07052977, 0.06947646, 0.06885928, 0.06852632, 0.06836134, 
0.06829559, 0.06804968, 0.06684561, 0.06508074, 0.06383415, 0.06333059, 
0.06309205, 0.06312215, 0.06308869, 0.06325907, 0.06330066, 0.06230686, 
0.06121331, 0.06093323, 0.06080826, 0.06103985, 0.06129866, 0.0616675, 
0.06222659, 0.06271791, 0.06269919, 0.06317165, 0.06388476, 0.06443688, 
0.06532656, 0.06643683, 0.06762666, 0.0688545, 0.06957003, 0.07049679, 
0.07145847, 0.07254429, 0.07379688, 0.07520389, 0.07666438, 0.07813754, 
0.07980724, 0.08164999, 0.08337331, 0.0850293, 0.08675431, 0.08850279, 
0.0903589, 0.09223478, 0.09399396, 0.09617301, 0.09825616, 0.1001754, 
0.10215286, 0.10405939, 0.10593522, 0.10771114, 0.10955779, 0.11137673, 
0.11350922, 0.11566091, 0.11786379, 0.1201627, 0.12245044, 0.12446617, 
0.12668717, 0.12880468, 0.13083965, 0.13320723, 0.13573529, 0.13813868, 
0.14067729, 0.14306904, 0.14548148, 0.14758988, 0.14929967, 0.150388, 
0.15233791, 0.15449043, 0.15652253, 0.15867107, 0.16075753, 0.16281015, 
0.16490422, 0.16620035, 0.16787185, 0.16964339, 0.17125645, 0.17307489, 
0.17497104, 0.1767696, 0.17835094, 0.1791379, 0.17976147, 0.18114665, 
0.18252681, 0.18401302, 0.18556376, 0.18716799, 0.18869627, 0.18947925, 
0.18952475, 0.19017635, 0.19119224, 0.19240457, 0.19406083, 0.19560736, 
0.19702311, 0.19838278, 0.19857232, 0.19853884, 0.19905365, 0.19978584, 
0.20052382, 0.20136617, 0.20214938, 0.20287189, 0.20312707, 0.20246537, 
0.20244905, 0.20259959, 0.20278233, 0.20327239, 0.20340601, 0.20375103, 
0.20409654, 0.2038635, 0.20327988, 0.20336974, 0.20360702, 0.20394714, 
0.20437293, 0.20460138, 0.20475748, 0.20456536, 0.20375752, 0.20371552, 
0.20368604, 0.20359299, 0.2035453, 0.20345831, 0.20340526, 0.20343742, 
0.20276403, 0.20228943, 0.20203541, 0.20188482, 0.2018925, 0.20187522, 
0.20192079, 0.20182329, 0.20151561, 0.20119683, 0.20101932, 0.20076922, 
0.20026171, 0.19982927, 0.19950271, 0.19908488, 0.19889168, 0.19908054, 
0.19908604, 0.19869895, 0.1984064, 0.1980564, 0.19761464, 0.19729775, 
0.19710955, 0.1974078, 0.19742712, 0.19735026, 0.19726095, 0.19695149, 
0.19679484, 0.19663087, 0.19647489, 0.19718868, 0.19785891, 0.19784996, 
0.19788255, 0.19757998, 0.19728665, 0.19721918, 0.19730429, 0.19846697, 
0.19968045, 0.19982629, 0.20010276, 0.20030209, 0.20027906, 0.2004303, 
0.20071957, 0.20170523, 0.20357136, 0.20445201, 0.20511229, 0.2053825, 
0.20552762, 0.2057181, 0.20584874, 0.20681113, 0.20865934, 0.20982285, 
0.21037306, 0.21086055, 0.21114743, 0.21141832, 0.21172704, 0.21270722, 
0.21425499, 0.21508047, 0.21540311, 0.21570137, 0.21566178, 0.2157706, 
0.2157407, 0.21586709, 0.21686926, 0.21775885, 0.21800305, 0.21835183, 
0.21890219, 0.21994038, 0.22143123, 0.22224599, 0.22288845, 0.22398168, 
0.22474541, 0.22512311, 0.22491746, 0.2245485, 0.22401106, 0.22340615, 
0.22256076, 0.22140816, 0.22045675, 0.21932106, 0.21813713, 0.21674703, 
0.21546952, 0.21415956, 0.21265213, 0.21120454, 0.20967419, 0.2082095, 
0.20655277, 0.20475774, 0.20279387, 0.20076135, 0.19890919, 0.19709851, 
0.19524029, 0.19323021, 0.19112383, 0.18902898, 0.18701997, 0.18506767, 
0.18315709, 0.18136762, 0.17967033, 0.1778329, 0.17634939, 0.17506276, 
0.17422849, 0.17365934, 0.17368531, 0.17453934, 0.17546247, 0.17564483, 
0.17587478, 0.17576717, 0.17500107, 0.1736709, 0.17258336, 0.17265072, 
0.17284319, 0.17171922, 0.16994849, 0.16780928, 0.16595082, 0.16508843
), Mark = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 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x) (x-5):(x+5)))
    – G5W
    Commented May 21, 2017 at 18:35

2 Answers 2

8

You don't need dplyr, you can use indexing in base R.

inds = which(df$Mark == 1)
# We use lapply() to get all rows for all indices, result is a list
rows <- lapply(inds, function(x) (x-5):(x+5))
# With unlist() you get all relevant rows
df[unlist(rows),]
1
  • This is a good solution, but how can we get rid of negative row index inside the lapply? Commented Jun 29, 2022 at 16:23
1

I just solved a similar problem with the shift function from data.table. Basically I use shift in the i statement of the data table:

library(data.table)

df[Mark == 1 | shift(Mark==1, n=5L, type = "lag") | shift(search==1, n=5L, type = "lead")]

I always find data.table to be really intuitive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.