julia> using DataFrames, Symbolics
julia> df_flows = DataFrame(;
from = ["p1", "p1", "p2", "p2", "p1", "p2"],
to = ["d", "d", "d", "d", "d", "d"],
rp = [1, 1, 1, 1, 2, 2],
tb = [3,5,7,4,6,8],
index = 1:6,
)
6×5 DataFrame
Row │ from to rp tb index
│ String String Int64 Int64 Int64
─────┼─────────────────────────────────────
1 │ p1 d 1 3 1
2 │ p1 d 1 5 2
3 │ p2 d 1 7 3
4 │ p2 d 1 4 4
5 │ p1 d 2 6 5
6 │ p2 d 2 8 6
julia> @variables x y z
3-element Vector{Num}:
x
y
z
julia> w=x^2+y*sqrt(z*x)
x^2 + y*sqrt(x*z)
julia> transform(df_flows,[3,4,5]=>ByRow((r,t,i)->substitute(w, Dict(x=>r,y=>t,z=>i))))
6×6 DataFrame
Row │ from to rp tb index rp_tb_index_function
│ String String Int64 Int64 Int64 Num
─────┼───────────────────────────────────────────────────────────
1 │ p1 d 1 3 1 4.0
2 │ p1 d 1 5 2 8.07107
3 │ p2 d 1 7 3 13.1244
4 │ p2 d 1 4 4 9.0
5 │ p1 d 2 6 5 22.9737
6 │ p2 d 2 8 6 31.7128
julia> w=3x-2y
3x - 2y
julia> transform(df_flows,[3,4,5]=>ByRow((r,t,i)->substitute(w, Dict(x=>r,y=>t,z=>i))))
6×6 DataFrame
Row │ from to rp tb index rp_tb_index_function
│ String String Int64 Int64 Int64 Num
─────┼───────────────────────────────────────────────────────────
1 │ p1 d 1 3 1 -3
2 │ p1 d 1 5 2 -7
3 │ p2 d 1 7 3 -11
4 │ p2 d 1 4 4 -5
5 │ p1 d 2 6 5 -6
6 │ p2 d 2 8 6 -10
PS
I saw several old posts that discussed the problem of deriving a function from a string. Apart from the solution with parse() and eval() I have seen the use of the getfield function applied to the current module to obtain the function from the string.
But this applies to functions already defined in the module.
An equivalent would be, in my opinion, a dict with strings as keys and functions as values.