"""
I am trying to generate a function that creates a DataFrame dynamically with number of columns determined by length of array arguments (n) to the function.
But I failed to generate the DataFrame dynamically because I don't understand how to use a `for loop` to generate DataFrame columns.
Noticed that the dataframe is defined in lines 19 through 30 and lines 25 through 28 are the old code that did not attempt dynamic generation of the DataFrame columns.
The attempted for loop in lines 21 through 23 does not work and something needs to replace it.
"""
{
function testf(x, y, z) # inputs are arrays measured over time periods of a year; by definition, x - y = z, or y = x - z
n = length(x)
td = today()
byr = year(td) - t # calcs the current base year
if sum(y) == 0 && mode(y) == 0 && length(z) == n # inner if for case where y is unkown and will be derived from x and z
y = y .+ (x .- z)
zx = zeros(n); # placeholder z to x ratio array
zy = zeros(n); # placeholder z to y ratio array
for i in 1:n
zx[i] = z[i]/x[i]; # filling the zx ratio array through nth element
zy[i] = z[i]/y[i]; # filling the zy ratio array through nth element
end # for i loop
ave_zx = mean(zx); ave_zy = mean(zy);
ave_zx_rnd = round(ave_zx,digits=6); ave_zy_rnd = round(ave_zy,digits=6);
df1 = DataFrame([:item => ["x", "y", "z", repeat("-", 12), "z/x", "z/y", "Ave(z)/Ave(x)", "Ave(z)/Ave(y)"], # begin dynamic DF generation based on n = length of data arrays
#
for j in 1:n
Symbol(byr-(j-1)) => [x[j], y[j], z[j], repeat("-", 12), round(zx[j],digits=6), round(zy[j],digits=6), "n/a", "n/a"],
end # for loop
#
# Symbol(byr-0) => [x[1], y[1], z[1], repeat("-", 12), round(zx1,digits=6), round(zy1,digits=6), "n/a", "n/a"], # this is previous code used to fill df1 before for loop idea
# Symbol(byr-1) => [x[2], y[2], z[2], repeat("-", 12), round(zx2,digits=6), round(zy2,digits=6), "n/a", "n/a"], # this is previous code used to fill df1 before for loop idea
# Symbol(byr-2) => [x[3], y[3], z[3], repeat("-", 12), round(zx3,digits=6), round(zy3,digits=6), "n/a", "n/a"], # this is previous code used to fill df1 before for loop idea
# Symbol("Ave($(byr-(2))-$(byr))") => [ave_x_rnd, ave_y_rnd, ave_z_rnd, repeat("-", 12), ave_zx_rnd, ave_zy_rnd, ave_z_to_ave_x_rnd, ave_z_to_ave_y_rnd] # this is previous code used to fill df1 before for loop idea
Symbol("Ave($(byr-(j-1))-$(byr))") => [ave_x_rnd, ave_y_rnd, ave_z_rnd, repeat("-", 12), ave_zx_rnd, ave_zy_rnd, ave_z_to_ave_x_rnd, ave_z_to_ave_y_rnd]
]) # end dynamic DF generation based on n = length of data arrays
println()
show(df1) # display the dynamic DataFrame generated by the for loop
println()
else
println("The function testf() has errors and cannot execute.")
end # end if sum(y) == 0...
end # end function testf()
}
"""
I'm trying to generate output like this for the case of n=3.
"""
β Row β item β 2020 β 2019 β 2018 β Ave(2018-2020) β
β β Any β Any β Any β Any β Any β
βββββββΌββββββββββββββββΌβββββββββββββββΌβββββββββββββββΌβββββββββββββββΌβββββββββββββββββ€
β 1 β x β 31323718 β 14342460 β 10353288 β 1.86732e7 β
β 2 β y β 30609098 β 14188281 β 10066719 β 1.8288e7 β
β 3 β z β 714620.0 β 154179.0 β 286569.0 β 3.85123e5 β
β 4 β ------------ β ------------ β ------------ β ------------ β ------------ β
β 5 β z/x β 0.022814 β 0.01075 β 0.027679 β 0.020414 β
β 6 β z/y β 0.023347 β 0.010867 β 0.028467 β 0.020893 β
β 7 β Ave(z)/Ave(x) β n/a β n/a β n/a β 0.020624 β
β 8 β Ave(z)/Ave(y) β n/a β n/a β n/a β 0.021059 β
![image|690x206](upload://hZVoNOa4yWWfAUe4DcUkkPcZHRk.png)