Need help writing some recursive code please! Full code explanation and snapshot included

Hi how’s it going?

I’m trying to write this recursive code out and can’t get it to work. Can someone help me with a solution? Here is a code of the scenario.

y = [2,2,2,2,3,3,4]
x = [1,2,3,3,2,2,2]
df = DataFrame(:column_1=>x,:column_2=>y)
df[!,:column_3] = string.(df[!,:column_1]).*string.(df[!,:column_2])

countmap(df[!,:column_3])

Dict{String,Int64} with 5 entries:
  "32" => 2
  "24" => 1
  "12" => 1
  "23" => 2
  "22" => 1

What I’m looking for is that no value of column_3 has a count of 1 in the dataframe. If it is the case, then it should look either forward or backwards to rename to another value of :column_3, based on the two values that it was concatenated from. What I’m doing now is sorting by column 1, then identifying which values of column 3 has a count of 1, then trying to run a loop and replacing values then calling the function recursively, but I can’t get it.

For example, in the image, “12” only has 1 value, it should then look upwards to see if there’s a “22”, and since there is, it should rename to “22”. Similarly, “24” only has one value. It should look backwards and find that there is no “14” value so it checks the second part of the string concatenation and finds that there is a “23” value so it renames it to that.

A little complicated but essentially I just dont want any values of column_3 to have a count of 1 in the dataframe and it should adjust based on the closest numerical values pre-string concatenation.

Thank you!

Please post code that people can copy and paste, not a screenshot.

2 Likes

Ok done, sorry bout that.

1 Like

Thanks. Please quote your code as well: PSA: make it easier to help you

Cool thanks for the link. Done.

1 Like

Thanks for the post.

Can you give a more complete MWE?

First, please quote your code using triple back ticks

```
like this
```

It isn’t super clear what you want from this. It would be helpful if your MWE could include information about what your desired output should be.

Here is a super basic implementation


julia> function change_all!(v)
       unique_items = unique(v)
       i = 1
       while no_uniques(v) && i <= length(unique_items)
           change!(v, unique_items[i])
           i += 1
       end
       return v
       end

julia> function no_uniques(v)
           !(1 in values(countmap(v)))
       end

julia> function change!(v, current)
       if count(t -> t == current, v) == 1
           similars = filter(t -> startswith(t, first(current)), v)
           similars = setdiff(similars, [current])
           sim1 = first(similars)
           v[v .== current] .= sim1
       end
       return v
       end

julia> v = ["21", "22", "22"]
julia> change_all!(v)
1 Like

Cool thanks a lot for the reply. I will try implementing this. It’s a bit difficult for me to describe it any further, tried to give an extremely simple example. But if I’m not able to make it work with the code you provided, I will figure out a way to describe it better.