# DataFrames version of R's which() function

**URL:** <https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [January 28, 2022, 11:48pm UTC](https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401 "2022-01-28T23:48:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwalters31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwalters31/32/25594_2.png) [@rwalters31](https://discourse.julialang.org/u/rwalters31)\
**Post date:** [January 28, 2022, 11:48pm UTC](https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401/1 "2022-01-28T23:48:27Z")

</div>

TLDR; is there a Julia version of R’s which() function?

I’m rewriting some code from R to Julia. I’ve gotten comfortable with the DataFrames subset function but in addition to returning rows I occassionally need to access relative to a specific row that meets a given condition. My data records a series of simulated aircraft flights. They always start at a given position but vary in end point and duration. To find the beginning of each run I subset the data as follows:

> @subset(df,:XPOSITION.== df.XPOSITION[1])

I’d like to be able to get the last row of each run as well. In R I would use the which() function which returns a vector of indices in the original dataframe that match the given condition. Then I know that the end positions abutt the start positions and I can shift the correct elements of the start position vector to get the end position vector.

Is there a Julia equivelant to R’s which()?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 28, 2022, 11:59pm UTC](https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401/2 "2022-01-28T23:59:30Z")

</div>

You can do

```julia
@subset(df, :XPOSITION .== :XPOSITION[1])

```

which works just fine (and will be faster)

I think you may want to use a grouped data frame? This might not be the most elegant solution, maybe someone else has an answer

```julia
julia> df = DataFrame(flight = [1, 1, 1, 1, 2, 2, 2, 2], location = [1, 2, 3, 2, 43, 44, 43, 42])
8×2 DataFrame
 Row │ flight location
     │ Int64 Int64
─────┼──────────────────
   1 │ 1 1
   2 │ 1 2
   3 │ 1 3
   4 │ 1 2
   5 │ 2 43
   6 │ 2 44
   7 │ 2 43
   8 │ 2 42

julia> @chain df begin
           groupby(:flight)
           combine(_) do sdf
               sdf[[1, nrow(sdf)], :]
           end
       end
4×2 DataFrame
 Row │ flight location
     │ Int64 Int64
─────┼──────────────────
   1 │ 1 1
   2 │ 1 2
   3 │ 2 43
   4 │ 2 42

```

If each data frame is one flight, it’s just

```julia
df[[1, nrow(df), :]

```

I can’t think of a great way to write it with DataFramesMeta.jl verbs, to be honest. Maybe someone else will think of something.

To just get the indices, I would recommend using `@with` rom DataFramesMeta.jl

```julia
@with df :XPOSITION .== :XPOSITION[1]

```

Some R code you use would also be helpful

---

<div class="post-metadata">

**Author:** ![rwalters31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwalters31/32/25594_2.png) [@rwalters31](https://discourse.julialang.org/u/rwalters31)\
**Post date:** [January 29, 2022, 1:46am UTC](https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401/3 "2022-01-29T01:46:20Z")

</div>

> [@pdeffebach](#):
>
> ```julia
> @with df :XPOSITION .== :XPOSITION[1]
> 
> ```

So at the moment my solution is

> row = 1:length(df.XPOSITION)  
> df.row = row  
> startRows = @subset(df, :XPOSITION.== :XPOSITION[1])  
> startIndex = startRows.row  
> endIndex = vcat(startIndex[2:end].-1,last(df.row))

This works but adding in a column of row indices when my data is 6.2+ million rows long seems like a really inefficient way of getting the indices I need.

Unfortunately the data does not have the indivdual flights labeled. That is what I’m going to use the start and end index lists to do.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [January 29, 2022, 3:26am UTC](https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401/4 "2022-01-29T03:26:12Z")

</div>

> [@rwalters31](#):
>
> > endIndex = vcat(startIndex[2:end].-1,last(df.row))

I’m not sure what this last line is doing. But it sounds like you need the function `findall`. You could do, for example

```julia
@with df findall(==(:XPOSITION[1]), :XPOSITION)

```

---

<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:** [January 29, 2022, 7:04am UTC](https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401/5 "2022-01-29T07:04:24Z")

</div>

Yes, I think @rwalters31 is looking for `findall`.

---

<div class="post-metadata">

**Author:** ![rwalters31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwalters31/32/25594_2.png) [@rwalters31](https://discourse.julialang.org/u/rwalters31)\
**Post date:** [January 29, 2022, 1:52pm UTC](https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401/6 "2022-01-29T13:52:34Z")

</div>

@pdeffebach & @bkamins findall is the function I was hunting for. Thanks!

---

<div class="post-metadata">

**Author:** ![rwalters31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwalters31/32/25594_2.png) [@rwalters31](https://discourse.julialang.org/u/rwalters31)\
**Post date:** [January 29, 2022, 1:59pm UTC](https://discourse.julialang.org/t/dataframes-version-of-rs-which-function/75401/7 "2022-01-29T13:59:17Z")

</div>

To get the index of the last row of I flight I know that it is the index one prior to the start of the next flight so for example if startIndex = [1,10,17,25] then the end index for flight 1 would be 9 (the index prior to the start of flight 2) and so forth so in this case endIndex = [9,16,24,nrow(df)].
