The following code chunk is written in R which I would like to replicate in Julia in order to name a variable using the current assigned value for a variable, which is going to change within an iterator:
Here is some Julia code for an example, with the way to do it in R, given in the comments:
using Random,Distributions,DataFrames
df = DataFrame(Matrix{Float64}(undef,100,5),:auto)
mu = 1.5:0.5:3.5
sigma = 2:0.5:4
for i in 1:5
df[:,i] = rand(Normal(mu[i],sigma[i]),100)
## here is the following line of code that I would like but
## given in R syntax:
# propertynames(df)[i] = paste("mu",mu[i],"sigma",sigma[i],sep="_")
end
To add to the answer above, rename! also accepts integers to specify the columns, so your loop would read like this:
for i in 1:5
rand!(Normal(mu[i],sigma[i]), df[!,i])
rename!(df, i => string("mu_", mu[i], "_sigma_", sigma[i]))
end
I also changed the first line in the loop, since your version allocates a new vector with each rand call, whereas rand! writes directly into the vector stored in the DataFrame (here, you need to use df[!,i] instead of df[:,i] to avoid copying the vector from the DataFrame). rand! is part of the Random stdlib.
Edit: fixed wrong order of arguments for rand! (see below)