# Using current variable values in the naming of DataFrame column(s)

**URL:** <https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [September 30, 2021, 8:33am UTC](https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982 "2021-09-30T08:33:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![conorhassan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/conorhassan/32/27857_2.png) [@conorhassan](https://discourse.julialang.org/u/conorhassan)\
**Post date:** [September 30, 2021, 8:33am UTC](https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982/1 "2021-09-30T08:33:39Z")

</div>

Hi there,

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:

```nohighlight
> mu = 0
> sigma = 1
> assign(paste("normal",mu,sigma,sep="_"),5)
> normal_0_1
[1] 5

```

Here is some Julia code for an example, with the way to do it in R, given in the comments:

```nohighlight
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

```

This would give the following output afterwards:

```nohighlight
julia> property_names(df}
5-element Vector{Symbol}:
 :mu_1.5_sigma_2.0
 :mu_2.0_sigma_2.5
 :mu_2.5_sigma_3.0
 :mu_3.0_sigma_3.5
 :mu_3.5_sigma_4.0

```

Any help would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [September 30, 2021, 8:49am UTC](https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982/2 "2021-09-30T08:49:38Z")

</div>

If the DataFrame column in question already exists, you can use `rename`:

```julia
julia> var = 1.0
1.0

julia> df = DataFrame(var=var)
1×1 DataFrame
 Row │ var
     │ Float64
─────┼─────────
   1 │ 1.0

julia> rename!(df, :var => "var_$var")
1×1 DataFrame
 Row │ var_1.0
     │ Float64
─────┼─────────
   1 │ 1.0

```

For the creation of new columns in an existing DataFrame, you can use:

```julia
julia> df = DataFrame()
0×0 DataFrame

julia> df[!, "var_$var"] = [1.0, 2.0]
2-element Vector{Float64}:
 1.0
 2.0

julia> df
2×1 DataFrame
 Row │ var_1.0
     │ Float64
─────┼─────────
   1 │ 1.0
   2 │ 2.0

```

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [September 30, 2021, 9:07am UTC](https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982/3 "2021-09-30T09:07:20Z")

</div>

To add to the answer above, `rename!` also accepts integers to specify the columns, so your loop would read like this:

```julia
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](https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982/6))

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 30, 2021, 10:51am UTC](https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982/4 "2021-09-30T10:51:36Z")

</div>

@sostock, the inplace `rand!()` command produces:  
`ERROR: StackOverflowError`

Using Julia 1.7, DataFrames v1.2.2  
Any idea about what is going on?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 30, 2021, 10:54am UTC](https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982/5 "2021-09-30T10:54:37Z")

</div>

DataFramesMeta can handle this, too (unlike dplyr).

```julia
for i in 1:5
    @transform df $"va$i" = rand(Normal(mu, sigma), 100)
end

```

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [September 30, 2021, 11:09am UTC](https://discourse.julialang.org/t/using-current-variable-values-in-the-naming-of-dataframe-column-s/68982/6 "2021-09-30T11:09:39Z")

</div>

Oh, I didn’t see that the `rand!` implementation in `Distributions` wants the distribution first and then the array. The arguments have to be switched:

```julia
rand!(Normal(mu[i],sigma[i]), df[!,i])

```

Edit: There is an [issue](https://github.com/JuliaStats/Distributions.jl/issues/1316) for switching the argument order to be consistent with `Random`, so this might change in a future Distributions.jl release.
