# Finding the position in array

**URL:** <https://discourse.julialang.org/t/finding-the-position-in-array/76430>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [February 14, 2022, 12:44pm UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430 "2022-02-14T12:44:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [February 14, 2022, 12:44pm UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/1 "2022-02-14T12:44:26Z")

</div>

```
myd = DataFrame(a = 1:3, b = ["aba","saba","daba"])

```

Now trying to find the position of :b elements in certain vector and make a new column out of it:

```
myd = transform(myd, :b => (v -> findfirst(x -> x==v, ["saba","daba","myy","aba","oba"])) => :sort_order)

```

I end up with column of nothing.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [February 14, 2022, 12:59pm UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/2 "2022-02-14T12:59:56Z")

</div>

It’s because your function is wrongly specified: You check if `x == v` where `x` is an element in the vector `v`.  
You could do

```julia
v2 = ["saba","daba","myy","aba","oba"]
transform(myd, :b => v -> [findfirst(isequal(i), v) for i in v2])

```

To be more efficient, you should probably create a `Dict{String, Int}` that maps strings to indices, then use that to look up. Something like

```julia
d = Dict(s => i for (i, s) in enumerate(v2))
myd.pos = [d[s] for s in myd.b]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 14, 2022, 1:31pm UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/3 "2022-02-14T13:31:22Z")

</div>

@Mastomaki, what result do you expect?

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [February 14, 2022, 5:26pm UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/4 "2022-02-14T17:26:40Z")

</div>

There should be a new column :sort\_order with values 4,1,2 because .e.g “aba” is the 4th element in  
[“saba”,“daba”,“myy”,“aba”,“oba”].

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 14, 2022, 6:23pm UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/5 "2022-02-14T18:23:32Z")

</div>

So you already have one working answer above.

I would do:

```julia
v = ["saba","daba","myy","aba","oba"]
myd.sortorder = [findfirst(==(x), v) for x in myd.b]

```

---

<div class="post-metadata">

**Author:** ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)\
**Post date:** [February 15, 2022, 3:57am UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/6 "2022-02-15T03:57:17Z")

</div>

```julia
myd = DataFrame(a = 1:3, b = ["aba","saba","daba"])
df = DataFrame(b = ["saba","daba","myy","aba","oba"], sort_order = 1:5)
leftjoin!(myd, df, on = :b)

```

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [February 15, 2022, 6:42am UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/7 "2022-02-15T06:42:06Z")

</div>

So what is the difference between isequal() and == ?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [February 15, 2022, 7:09am UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/8 "2022-02-15T07:09:34Z")

</div>

@Mastomaki  
From the documentation:

> isequal(x, y)
> 
> Similar to ==, except for the treatment of floating point numbers and of missing values. isequal treats all floating-point NaN  
> values as equal to each other, treats -0.0 as unequal to 0.0, and missing as equal to missing. Always returns a Bool value.

Basically, `isequal` is what you would _intutively_ expect equality to be. For numeric purposes or data analysis purposes, `NaN`, 0.0 and -0.0 (which are different bit patterns), and `missing` have specific defined behavior that makes them behave differently from other values. `==` follows that defined behaviour (`NaN == NaN` is false, for eg.), which is useful for those purposes.

But for other purposes, like using them as keys in a `Dict`, you usually want them behave like any other value, and that’s why `isequal` exists and is used by `Dict`. `isequal(NaN, NaN)` returns true.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [February 15, 2022, 7:12am UTC](https://discourse.julialang.org/t/finding-the-position-in-array/76430/9 "2022-02-15T07:12:51Z")

</div>

In this case though, that’s not what makes the difference. The code could have as well done `[findfirst(==(i), v) for i in v2]` and behaved the same. The difference here is that you were comparing x to the whole of v, which is obviously false, whereas here we’re iterating over v and comparing x to its individual values (strings).
