Perhaps what you were trying to do could be done in the following way, it being understood that the solution of @digital_carver is the βGoodβ one.
julia> df = DataFrame(
pdb_names = ["C1_1", "C1_1", "C1_2", "C1_3", "C2_1", "C2_2"],
missings_range = [[2,5],[9,10], [9,10], [1,4], [1,4],[1,4]],
seqs_length = [10,10,10,7,7,7])
6Γ3 DataFrame
Row β pdb_names missings_range seqs_length
β String Vector{Int64} Int64
ββββββΌββββββββββββββββββββββββββββββββββββββββ
1 β C1_1 [2, 5] 10
2 β C1_1 [9, 10] 10
3 β C1_2 [9, 10] 10
4 β C1_3 [1, 4] 7
5 β C2_1 [1, 4] 7
6 β C2_2 [1, 4] 7
julia> insertcols!(df, :x => repeat.(Ref([0]),df.seqs_length))
# or
julia> insertcols!(df, :x => fill.(0,df.seqs_length))
6Γ4 DataFrame
Row β pdb_names missings_range seqs_length x
β String Vector{Int64} Int64 Vector{Int64}
ββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β C1_1 [2, 5] 10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
2 β C1_1 [9, 10] 10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
3 β C1_2 [9, 10] 10 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
4 β C1_3 [1, 4] 7 [0, 0, 0, 0, 0, 0, 0]
5 β C2_1 [1, 4] 7 [0, 0, 0, 0, 0, 0, 0]
6 β C2_2 [1, 4] 7 [0, 0, 0, 0, 0, 0, 0]
julia> combine(df, [:x, :missings_range]=>ByRow((x,y)->x[range(y...)].=1))
6Γ1 DataFrame
Row β x_missings_range_function
β SubArrayβ¦
ββββββΌβββββββββββββββββββββββββββ
1 β [1, 1, 1, 1]
2 β [1, 1]
3 β [1, 1]
4 β [1, 1, 1, 1]
5 β [1, 1, 1, 1]
6 β [1, 1, 1, 1]
julia> df
6Γ4 DataFrame
Row β pdb_names missings_range seqs_length x
β String Vector{Int64} Int64 Vector{Int64}
ββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1 β C1_1 [2, 5] 10 [0, 1, 1, 1, 1, 0, 0, 0, 0, 0]
2 β C1_1 [9, 10] 10 [0, 0, 0, 0, 0, 0, 0, 0, 1, 1]
3 β C1_2 [9, 10] 10 [0, 0, 0, 0, 0, 0, 0, 0, 1, 1]
4 β C1_3 [1, 4] 7 [1, 1, 1, 1, 0, 0, 0]
5 β C2_1 [1, 4] 7 [1, 1, 1, 1, 0, 0, 0]
6 β C2_2 [1, 4] 7 [1, 1, 1, 1, 0, 0, 0]
Regarding the result of these expressions I ask who can explain why the contents of the column :x have been mutated.
A different solution β¦ without insertcols!()
transform(df, [:missings_range, :seqs_length]=>ByRow(((y,x)-> [e in range(y...) ? 1 : 0 for e in 1:x] ))
)
transform(df, [:missings_range, :seqs_length]=>ByRow(((y,x)-> [e in range(y...) ? true : false for e in 1:x] )))
transform(df, [:missings_range, :seqs_length]=>ByRow((x,y)->reverse(bitstring(sum((^).(2,range(x...).-1))))[1:y])=>:rngs)