How to correctly create ''slices'' of a column by using indexes of another one, in a DataFrame

Is this what you want?

julia> df
3×2 DataFrame
 Row │ i1                             i2
     │ Array…                         Array…
─────┼──────────────────────────────────────────
   1 │ [1, 2, 3, 4, 5, 1, 2]          [4, 2, 1]
   2 │ [1, 2, 3, 4, 5, 6, 8, 10, 11]  [3, 3, 3]
   3 │ [1, 2, 3, 4, 5]                [1, 2, 2]

julia> function cutv(x,idx)
           ends = cumsum(idx)
           starts = [1; ends[1:end-1] .+ 1]
           return [x[s:e] for (s,e) in zip(starts, ends)]
       end
cutv (generic function with 1 method)

julia> function trans(xs, idxs)
           newvs = [cutv(xs[i], idxs[i]) for i in eachindex(xs, idxs)]
           return [[v[i] for v in newvs] for i in eachindex(newvs...)]
       end
trans (generic function with 1 method)

julia> df.v1, df.v2, df.v3 = trans(df.i1, df.i2);

julia> df
3×5 DataFrame
 Row │ i1                             i2         v1            v2         v3
     │ Array…                         Array…     Array…        Array…     Array…
─────┼────────────────────────────────────────────────────────────────────────────────
   1 │ [1, 2, 3, 4, 5, 1, 2]          [4, 2, 1]  [1, 2, 3, 4]  [5, 1]     [2]
   2 │ [1, 2, 3, 4, 5, 6, 8, 10, 11]  [3, 3, 3]  [1, 2, 3]     [4, 5, 6]  [8, 10, 11]
   3 │ [1, 2, 3, 4, 5]                [1, 2, 2]  [1]           [2, 3]     [4, 5]
2 Likes