dataframejuliadataframes.jl

Replace duplicates with new values in dataframe


I have a data frame with some repeat values in one of the columns:

df = DataFrame(A=["a","a","b"])

What I would like to do is replace all values with unique values and an incremental ordering if there are repeat values, like so:

df = DataFrame(A=["a_1","a_2","b_1"])

I know in R you can do this with make.unique, but is there an equivalent function in Julia?


Solution

  • If you want exactly this output there is no ready function for this as far as I can tell, but you can do e.g.

    julia> transform(groupby(df, :A), :A => (x -> string.(x, "_", 1:length(x))) => :A_unique)
    3×2 DataFrame
     Row │ A       A_unique
         │ String  String
    ─────┼──────────────────
       1 │ a       a_1
       2 │ a       a_2
       3 │ b       b_1