# Creating ranges from findall

**URL:** <https://discourse.julialang.org/t/creating-ranges-from-findall/43885>\
**Category:** General Usage\
**Created:** [July 29, 2020, 2:34pm UTC](https://discourse.julialang.org/t/creating-ranges-from-findall/43885 "2020-07-29T14:34:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [July 29, 2020, 2:34pm UTC](https://discourse.julialang.org/t/creating-ranges-from-findall/43885/1 "2020-07-29T14:34:46Z")

</div>

Do you know of a way to turn the results of findall into Vector{UnitRange{Int64}}?

```julia
searching = DataFrame(A = 1:5:1000)

searching_specific = searching[:,:A]

findall(x -> isodd(x), searching_specific)

```

The results look like: 1, 3, 5, 7, 9, 11 etc.

I want to be able to create ranges from this like

```julia
[1:2]
[3:4]
[5:6]

```

Creating a range using a value and -1 from the next value point.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 29, 2020, 2:46pm UTC](https://discourse.julialang.org/t/creating-ranges-from-findall/43885/2 "2020-07-29T14:46:35Z")

</div>

I’m not at my computer, so your mwe confuses me a bit. What is the purpose of the DataFrame here? What does `searching_specific` contain? Can you create it without going through the DataFrame, for clarity?

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [July 29, 2020, 2:50pm UTC](https://discourse.julialang.org/t/creating-ranges-from-findall/43885/3 "2020-07-29T14:50:44Z")

</div>

```julia
A = 1:5:1000
F = findall(isodd, A)
R = [i:j-1 for (i, j) in zip(F[1:end-1], F[2:end])]

```

There’s probably a better way, but this does what you want.

---

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [July 29, 2020, 3:09pm UTC](https://discourse.julialang.org/t/creating-ranges-from-findall/43885/4 "2020-07-29T15:09:43Z")

</div>

```julia
inds = findall(isodd, searching_specific)
ranges = @views (:).(inds[1:end-1], inds[2:end] .- 1)

```

This allocates, but it seems not to be a problem since you are using `findall`. If you want an allocation-free alternative, you may create a new iterable type like this:

```julia
struct FindRanges{F, T}
	f::F
	t::T
end
function Base.iterate(x::FindRanges, i1 = findfirst(x.f, x.t))
	i2 = findnext(x.f, x.t, nextind(x.t, i1))
	if i2 === nothing
		nothing
	else
		i1 : prevind(x.t, i2), i2
	end
end

ranges2 = FindRanges(isodd, searching_specific)

```

`ranges2` is a iterable that yields the same as `ranges`, but without allocations.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [July 30, 2020, 8:59am UTC](https://discourse.julialang.org/t/creating-ranges-from-findall/43885/5 "2020-07-30T08:59:09Z")

</div>

Thank you for your replies.

@DNF Maybe it doesn’t need to be in a `DataFrame`. I have a long file of outputs from the same DifferentialEquation that I want to plot individually. I harvest a different species from the same food web, and save the outputs one after the other. `searching_specific` is when the time step is 0.0 (different from the MWE). I want the indices of where the 0.0 are, and create ranges from them so I can create plots. I keep it as `DataFrame` because I find `:Symbol` practical for plotting.

![image](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae9abec59ca36f9261054a9870e4647891eca9e3.png)

I don’t know if plotting this way will work, but now I can try. Thank you
