# Help needed regarding making list, and modifying them, inside a DataFrame

**URL:** <https://discourse.julialang.org/t/help-needed-regarding-making-list-and-modifying-them-inside-a-dataframe/87231>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [September 13, 2022, 7:27pm UTC](https://discourse.julialang.org/t/help-needed-regarding-making-list-and-modifying-them-inside-a-dataframe/87231 "2022-09-13T19:27:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_Mac\_Donagh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan_mac_donagh/32/31798_2.png) [@Juan\_Mac\_Donagh](https://discourse.julialang.org/u/Juan_Mac_Donagh)\
**Post date:** [September 13, 2022, 7:27pm UTC](https://discourse.julialang.org/t/help-needed-regarding-making-list-and-modifying-them-inside-a-dataframe/87231/1 "2022-09-13T19:27:05Z")

</div>

Hi all. I need helping figuring out how to make this:

I have a DataFrame that look like this:

```julia
	df = DataFrame()
	df.pdb_names = ["C1_1", "C1_1", "C1_2", "C1_3", "C2_1", "C2_2"]
	df.missings_range = [[2,5],[9,10], [9,10], [1,4], [1,4],[1,4]]
	df.seqs_length = [10,10,10,7,7] 

```

And I have been struggling with two things:  
The first one, I need to create a **new column** that is composed by a list of 0, where the length of said list is determined by `seqs_length `, so the first ones should be a list of 10 0’s, and so on.

For this, I tried doing something like this:  
`insertcols!(df2, :x => repeat([0],inner =df.seq_length, outer= 5))`, but I guess it is not the correct way of doing it, because I am getting an ` OutOfMemoryError()` Also, here I am using outer as a number, but I want to do it for the whole DF, that stores a bunch of data.

Secondly, I was trying to figure out a way to replace this list of 0’s within certain range. I know that I can do something with `replace.()`, but the issue is that I need to replace the 0’s for the positions that are delimited by the `df.missings_range` column, so the first row should have a list that looks like this: `[0,1,1,1,1,0,0,0,0,0]`. Here I’m at a total loss, so any help is welcome.

The expected result (with the last column) shlould look like this:

```julia
|pdb_names |ranges_missing|
|----------------------------|
|"C1_1"|[0,1,1,1,1,0,0,0,0,0]|
|"C1_1"|[0,0,0,0,0,0,0,0,1,1]|

```

Thanks a lot!

---

<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:** [September 13, 2022, 7:59pm UTC](https://discourse.julialang.org/t/help-needed-regarding-making-list-and-modifying-them-inside-a-dataframe/87231/2 "2022-09-13T19:59:55Z")

</div>

Let’s define a helper function first, that creates the output array you want for each row:

```julia
julia> function indicate_missings(missings_range, seqs_length)  
         v = zeros(Bool, seqs_length)
         v[first(missings_range):last(missings_range)] .= true
         v
       end
indicate_missings (generic function with 1 method)

```

This takes a single `missings_range` element and a single `seqs_length` value and returns the desired vector for it. You can test it with, for eg.:

```julia
julia> indicate_missings([2, 5], 10) |> println
Bool[0, 1, 1, 1, 1, 0, 0, 0, 0, 0]

```

Then, you can do a `transform` like this:

```julia
julia> transform(df, 
         [:missings_range, :seqs_length] => ByRow(indicate_missings) => :ranges_missing)
6×4 DataFrame
 Row │ pdb_names missings_range seqs_length ranges_missing                    
     │ String Vector{Int64} Int64 Vector{Bool}                      
─────┼───────────────────────────────────────────────────────────────────────────
   1 │ C1_1 [2, 5] 10 Bool[0, 1, 1, 1, 1, 0, 0, 0, 0, …
   2 │ C1_1 [9, 10] 10 Bool[0, 0, 0, 0, 0, 0, 0, 0, 1, …
   3 │ C1_2 [9, 10] 10 Bool[0, 0, 0, 0, 0, 0, 0, 0, 1, …
   4 │ C1_3 [1, 4] 7 Bool[1, 1, 1, 1, 0, 0, 0]
   5 │ C2_1 [1, 4] 7 Bool[1, 1, 1, 1, 0, 0, 0]
   6 │ C2_2 [1, 4] 7 Bool[1, 1, 1, 1, 0, 0, 0]

```

If you have questions about any part of this, please feel free to ask!

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [September 14, 2022, 10:50am UTC](https://discourse.julialang.org/t/help-needed-regarding-making-list-and-modifying-them-inside-a-dataframe/87231/3 "2022-09-14T10:50:15Z")

</div>

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
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!()`

```julia

transform(df, [:missings_range, :seqs_length]=>ByRow(((y,x)-> [e in range(y...) ? 1 : 0 for e in 1:x] ))
)

```

```julia

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)

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 14, 2022, 1:45pm UTC](https://discourse.julialang.org/t/help-needed-regarding-making-list-and-modifying-them-inside-a-dataframe/87231/4 "2022-09-14T13:45:45Z")

</div>

How about this?

```julia
julia> using DataFramesMeta;

julia> @rtransform df :missing_ranges = begin
           x = zeros(Int, :seqs_length)
           mi = :missings_range[1]
           mx = :missings_range[2]
           x[mi:mx] .= 1
           x
       end
6×4 DataFrame
 Row │ pdb_names missings_range seqs_length missing_ranges ⋯
     │ String Vector{Int64} Int64 Vector{Int64} ⋯
─────┼─────────────────────────────────────────────────────────────────────
   1 │ C1_1 [2, 5] 10 [0, 1, 1, 1, 1, 0, 0, 0, 0 ⋯
   2 │ C1_1 [9, 10] 10 [0, 0, 0, 0, 0, 0, 0, 0, 1
   3 │ C1_2 [9, 10] 10 [0, 0, 0, 0, 0, 0, 0, 0, 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]
                                                           1 column omitted

```

---

<div class="post-metadata">

**Author:** ![Juan\_Mac\_Donagh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan_mac_donagh/32/31798_2.png) [@Juan\_Mac\_Donagh](https://discourse.julialang.org/u/Juan_Mac_Donagh)\
**Post date:** [September 14, 2022, 2:45pm UTC](https://discourse.julialang.org/t/help-needed-regarding-making-list-and-modifying-them-inside-a-dataframe/87231/5 "2022-09-14T14:45:56Z")

</div>

Hi all! Thanks a lot. All the solutions worked great. Next time I should directly try to create a function, instead of trying to solve it only using the package and base. I marked the first one as the solution, only because it was the first one!

Thanks again, cheers

---

<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:** [September 14, 2022, 3:03pm UTC](https://discourse.julialang.org/t/help-needed-regarding-making-list-and-modifying-them-inside-a-dataframe/87231/6 "2022-09-14T15:03:30Z")

</div>

> [@Juan\_Mac\_Donagh](#):
>
> Next time I should directly try to create a function, instead of trying to solve it only using the package and base

Sometimes using only the package and base might be simpler:

```julia
df.ranges_missing = zeros.(Int, df.seqs_length)
for r in eachrow(df)
    mi, mx = r.missings_range
    r.ranges_missing[mi:mx] .= 1
end

```
