Dynamic name of DataFrames by iteration

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

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], ...

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.

1 Like

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

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.

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.

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

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

Thanks, I will try your suggestion.

Regards