# Dynamic name of DataFrames by iteration

**URL:** <https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 3, 2020, 2:08pm UTC](https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465 "2020-03-03T14:08:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alfre\_chile77](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Alfre\_chile77](https://discourse.julialang.org/u/Alfre_chile77)\
**Post date:** [March 3, 2020, 2:08pm UTC](https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465/1 "2020-03-03T14:08:41Z")

</div>

Hi,  
I want to create seven different Data Frames with different names tbl1:tbl7 based on the iteration over groupby()[i]

n1=7  
for i in 1:n1  
global “tbl$(i)”  
“tbl$(i)”= groupby(d4,:variable)[i]  
println(first(“tbl$(i)”,3))  
end

I receive a message:  
syntax: invalid syntax in “global” declaration

How could I obtain those seven different Data Frames to perform more evaluations individually (and plot them as well).

Regards

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 3, 2020, 2:53pm UTC](https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465/2 "2020-03-03T14:53:54Z")

</div>

May be it is acceptable to use the result of `groupby` by itself? Just declare `tbl = groupby(d4, variable)` and use it as `tbl[1], tbl[2], ...`

---

<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:** [March 3, 2020, 2:58pm UTC](https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465/3 "2020-03-03T14:58:06Z")

</div>

I think you are making this harder than it needs to be.

Are you coming from Stata by chance? This is a very stata-esque way of working with data.

My recommendation is to not try and automatically generate the names of the dataframe variables in global scope and instead use a container, like a `NamedTuple`. Trying to generate variable names on the fly like that is code smell.

But we would appreciate more information on what you are trying to accomplish, and could maybe point you in a different direction based on that.

---

<div class="post-metadata">

**Author:** ![Alfre\_chile77](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Alfre\_chile77](https://discourse.julialang.org/u/Alfre_chile77)\
**Post date:** [March 3, 2020, 2:58pm UTC](https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465/4 "2020-03-03T14:58:12Z")

</div>

Thanks for your answer, but the main reason is that I want to generate a dynamic structure for different DataFrames, and some have a lot of variables to do it manually

---

<div class="post-metadata">

**Author:** ![Alfre\_chile77](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Alfre\_chile77](https://discourse.julialang.org/u/Alfre_chile77)\
**Post date:** [March 3, 2020, 3:04pm UTC](https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465/5 "2020-03-03T15:04:45Z")

</div>

Thanks for your suggestion.  
My back ground is using R,  
What I want to accomplish is that I have a table with x amount of variables in long format, and I want to obtain a different table for each of the variables in order to perform different evaluations and make subplots with different titles per variable, and combine them into a single plot.

---

<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:** [March 3, 2020, 3:11pm UTC](https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465/6 "2020-03-03T15:11:04Z")

</div>

I see. you are trying to emulate the behavior of `ggplot`.

My suggestion would be to write a function that accepts a data-frame as input, does calculations, and produces a plot.

```julia
p = plot()
gd = groupby(df, :groupvar)
for sdf in gd
    sp = analysis_and_plot(sdf)
    plot!(p, sp) 
end

```

But there are other alternatives. You may want to perform the calculation using `by` and keep your data in long format, then use `StatsPlots` to emulate ggplor grammar of graphics. Something like

```julia
by(df, :groupvar) do sdf
    analysis(sdf) # produces a data frame
end

```

---

<div class="post-metadata">

**Author:** ![Alfre\_chile77](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Alfre\_chile77](https://discourse.julialang.org/u/Alfre_chile77)\
**Post date:** [March 3, 2020, 3:13pm UTC](https://discourse.julialang.org/t/dynamic-name-of-dataframes-by-iteration/35465/7 "2020-03-03T15:13:37Z")

</div>

Thanks, I will try your suggestion.

Regards
