# Recursive for loop to create data frame

**URL:** <https://discourse.julialang.org/t/recursive-for-loop-to-create-data-frame/26747>\
**Category:** General Usage\
**Created:** [July 24, 2019, 3:17pm UTC](https://discourse.julialang.org/t/recursive-for-loop-to-create-data-frame/26747 "2019-07-24T15:17:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [July 24, 2019, 3:17pm UTC](https://discourse.julialang.org/t/recursive-for-loop-to-create-data-frame/26747/1 "2019-07-24T15:17:11Z")

</div>

I have some JuMP (optimisation package) variables which I want to save to a data frame to then save to a .csv file. My idea is to have a data frame that would look something like this:

```julia
df = DataFrame(JuMPVariable = Symbol[], Value = Number[],
        Dimensions = Tuple[])

```

I would then implement a for loop to push all the JuMP variables to the data frame. However, the dimensions of the JuMP variables vary, so I can’t just do:

```julia
for d = 1:length(jump_var)
     push!(df, symbol, jump_var[d], d)

```

In fact what I’ve written above wouldn’t work, I just can’t even begin to think about how I would do this. I think I need some sort of recursion, but have never done that before (at least not consciously).

If it helps, I would like my output to look something like this:

```julia
│ Row │ JuMPVariable │ Value │ Dimensions │
│ │ Symbol │ Number │ Tuple │
├─────┼──────────────┼────────┼─────────────┤
│ 1 │ Capacity │ 1000 │ ("Wind",) │
│ 2 │ OPEX │ 100 │ ("Wind", 1) │
│ 3 │ OPEX │ 100 │ ("Wind", 2) │
│ 4 │ OPEX │ 100 │ ("Wind", 3) │
│ 5 │ OPEX │ 100 │ ("Wind", 4) │
│ 6 │ OPEX │ 100 │ ("PV", 1) │

```

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [July 24, 2019, 3:30pm UTC](https://discourse.julialang.org/t/recursive-for-loop-to-create-data-frame/26747/2 "2019-07-24T15:30:57Z")

</div>

You just want something like:

```julia
for d = 1:length(jump_var)
    push!(df.JuMPVariable, symbol)
    push!(df.Value, jump_var[d])
    push!(df.Dimensions, d)
end

```

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [July 24, 2019, 4:31pm UTC](https://discourse.julialang.org/t/recursive-for-loop-to-create-data-frame/26747/3 "2019-07-24T16:31:36Z")

</div>

I don’t, because `jump_var` is an array with 1, 2, 3, or 4 dimensions. I could write an if statement that would go something like:

```nohighlight
    for d1 =1:length(jump_var)
         do stuff
    end
elseif length(size(jump_var)) == 2
     for d1 = 1:size(jump_var)[1]
          for d2 = 1:size(jump_var)[2]
               do stuff
          end
      end

end

```

But that’s obviously inconvenient. (The code above probably doesn’t work, I’m on my phone and that’s just what I think it should look like but I haven’t tested it).

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [July 25, 2019, 8:14am UTC](https://discourse.julialang.org/t/recursive-for-loop-to-create-data-frame/26747/4 "2019-07-25T08:14:30Z")

</div>

For now I have decided to go with:

```julia
for (i,v) in enumarate(jump_var)
    push!(df.JuMPVariable, symbol)
    push!(df.Value, v)
    push!(df.Index, i)
end

```

Then I’ll have a data frame that I can write to a .csv file. I’m implicitly assuming that when I read from the csv file after I know the dimensions and so can use `reshape` to get my original array, but that’s not a problem. This might end up being a better way of doing it in any case.
