# How to delete range of values before and after a value in a column based on a condition from another column in a DataFrame?

**URL:** <https://discourse.julialang.org/t/how-to-delete-range-of-values-before-and-after-a-value-in-a-column-based-on-a-condition-from-another-column-in-a-dataframe/86577>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [August 30, 2022, 11:24pm UTC](https://discourse.julialang.org/t/how-to-delete-range-of-values-before-and-after-a-value-in-a-column-based-on-a-condition-from-another-column-in-a-dataframe/86577 "2022-08-30T23:24:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![akhibovska](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akhibovska/32/38256_2.png) [@akhibovska](https://discourse.julialang.org/u/akhibovska)\
**Post date:** [August 30, 2022, 11:24pm UTC](https://discourse.julialang.org/t/how-to-delete-range-of-values-before-and-after-a-value-in-a-column-based-on-a-condition-from-another-column-in-a-dataframe/86577/1 "2022-08-30T23:24:05Z")

</div>

Hi everyone,

I have a data frame where one column, “Blink” indicates with 1 if blink was present during a recording and with 0 if it was not. My column “Value” stores the actual data I am using. I successfully filtered the column “Value” if “Blink” is equal to 1, thus, filtering values that happened during a blink.

I am also attempting to remove 12 values in the column “Value” before the first filtered value and 12 values after the last filtered value. Here is an example of what I am hoping for it to look like (if I wanted to filter 2 preceding and exceeding values):

 ![Screen Shot 2022-08-30 at 7.20.35 PM](https://global.discourse-cdn.com/julialang/original/3X/0/7/072da0b6c0c535604f74298c6af7daa45391e55a.png)

I am unsure how to write a filter for the specific range before and after based on a condition from another column.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 31, 2022, 8:19am UTC](https://discourse.julialang.org/t/how-to-delete-range-of-values-before-and-after-a-value-in-a-column-based-on-a-condition-from-another-column-in-a-dataframe/86577/2 "2022-08-31T08:19:16Z")

</div>

assume your data frame is `df` and you want to remove up to two elements before and to after (you can change 2 to 12 later):

```julia
julia> using DataFrames, ShiftedArrays

julia> df = DataFrame(blink = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0], id=1:10)
10×2 DataFrame
 Row │ blink id
     │ Int64 Int64
─────┼──────────────
   1 │ 1 1
   2 │ 0 2
   3 │ 0 3
   4 │ 0 4
   5 │ 0 5
   6 │ 0 6
   7 │ 1 7
   8 │ 0 8
   9 │ 0 9
  10 │ 0 10

julia> subset(df, [:blink => x -> lag(x, i) .!== 1 for i in -2:2])
2×2 DataFrame
 Row │ blink id
     │ Int64 Int64
─────┼──────────────
   1 │ 0 4
   2 │ 0 10

```
