I was wondering what was the most efective way of generating many DataFrames at once while on a loop.
I can't figure out how, but the output should be something like this:
df_1 = 1×3 DataFrame
│ Row │ var1 │
│ │ String │
├─────┼──────────────┼
│ 1 │ b │
│ 2 │ a │
│ 3 │ c │
df_2 = 1×3 DataFrame
│ Row │ var1 │
│ │ String │
├─────┼──────────────┼
│ 1 │ h │
│ 2 │ j │
│ 3 │ k │
etc etc
So, the name of the DataFrame
should determined by the iteration of the loop (df_1, df_2
).
Any help is welcome! Thank in advance.
CodePudding user response:
I would recommend you to use e.g. a comprehension instead like this:
julia> df = [DataFrame(var1=rand(3)) for i in 1:3]
3-element Vector{DataFrame}:
3×1 DataFrame
Row │ var1
│ Float64
─────┼──────────
1 │ 0.402916
2 │ 0.562662
3 │ 0.256056
3×1 DataFrame
Row │ var1
│ Float64
─────┼──────────
1 │ 0.978262
2 │ 0.898357
3 │ 0.71393
3×1 DataFrame
Row │ var1
│ Float64
─────┼──────────
1 │ 0.634487
2 │ 0.813718
3 │ 0.113087
Then you can select the data frame you want using indexing, e.g.:
julia> df[1]
3×1 DataFrame
Row │ var1
│ Float64
─────┼──────────
1 │ 0.402916
2 │ 0.562662
3 │ 0.256056
julia> df[2]
3×1 DataFrame
Row │ var1
│ Float64
─────┼──────────
1 │ 0.978262
2 │ 0.898357
3 │ 0.71393
julia> df[3]
3×1 DataFrame
Row │ var1
│ Float64
─────┼──────────
1 │ 0.634487
2 │ 0.813718
3 │ 0.113087
Defining variables with dynamic naming the way you asked for can be done but I would not recommend it as it can easily lead to bugs in your code.