How about new feature of `:auto` symbol of `DataFrame`?

julia> DataFrame([1 0; 2 0], :auto)
2×2 DataFrame
 Row │ x1     x2    
     │ Int64  Int64
─────┼──────────────
   1 │     1      0
   2 │     2      0

Recently, the :auto symbol can be used to above result. I want new feature like this:

julia> x = [1,0]
2-element Vector{Int64}:
 1
 0

julia> y = [2,0]
2-element Vector{Int64}:
 2
 0

julia> DataFrame(x, y, :auto)
2×2 DataFrame
 Row │ x      y     
     │ Int64  Int64
─────┼──────────────
   1 │     1      0
   2 │     2      0

For example, below long assignment frequently used in my work:

        time_evolution = DataFrame(
n_S_ = n_S_, n_E_ = n_E_, n_I_ = n_I_, n_R_ = n_R_, n_V_ = n_V_, n_hub_ = n_hub_, RT_ = RT_,
S_influx_ = S_influx_, I_influx_ = I_influx_, E_influx_ = E_influx_, R_influx_ = R_influx_, V_influx_ = V_influx_,
S_outflux_ = S_outflux_, I_outflux_ = I_outflux_, E_outflux_ = E_outflux_, R_outflux_ = R_outflux_, V_outflux_ = V_outflux_
        )

I wanna write that like below:

        time_evolution = DataFrame(
n_S_, n_E_, n_I_, n_R_, n_V_, n_hub, RT_,
S_influx_, I_influx_, E_influx_, R_influx_, V_influx_,
S_outflux_, I_outflux_, E_outflux_, R_outflux_, V_outflux_,
:auto
        )

How about that?

Your two examples are different: the first one DataFrame(n_S = n_S, ...) will have column names matching the names of your inputs. You can do that with a NamedTuple:

julia> x = rand(5); y = rand(5);

julia> DataFrame((; x, y))
5×2 DataFrame
 Row │ x         y        
     │ Float64   Float64  
─────┼────────────────────
   1 │ 0.67518   0.269185
   2 │ 0.173524  0.554611
   3 │ 0.43453   0.241942
   4 │ 0.133013  0.273849
   5 │ 0.977161  0.822356

If you want :auto, just concatenate to a matrix:


julia> DataFrame([x, y], :auto)
5×2 DataFrame
 Row │ x1        x2       
     │ Float64   Float64  
─────┼────────────────────
   1 │ 0.67518   0.269185
   2 │ 0.173524  0.554611
   3 │ 0.43453   0.241942
   4 │ 0.133013  0.273849
   5 │ 0.977161  0.822356
5 Likes

Functions don’t have access to the names of input variables. There could be a macro to do this, though.

(The reason nilshg’s suggestion works is that (; x ) is interpreted as (; x=x ), so the tuple stores the variable name)

2 Likes

If this gives the result you need:

DataFrame(n_S_ = n_S_, n_E_ = n_E_, n_I_ = n_I_, <...>)

then this should be equivalent:

DataFrame(; n_S_, n_E_, n_I_, <...>)
1 Like