# Creation of R formula with interpolated Julia Symbol (RCall)

**URL:** <https://discourse.julialang.org/t/creation-of-r-formula-with-interpolated-julia-symbol-rcall/2240>\
**Category:** Statistics\
**Tags:** question\
**Created:** [February 22, 2017, 7:24pm UTC](https://discourse.julialang.org/t/creation-of-r-formula-with-interpolated-julia-symbol-rcall/2240 "2017-02-22T19:24:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego\_Javier\_Zea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diego_javier_zea/32/1858_2.png) [@Diego\_Javier\_Zea](https://discourse.julialang.org/u/Diego_Javier_Zea)\
**Post date:** [February 22, 2017, 7:24pm UTC](https://discourse.julialang.org/t/creation-of-r-formula-with-interpolated-julia-symbol-rcall/2240/1 "2017-02-22T19:24:09Z")

</div>

I have Julia `Symbols` with the name of the columns and I want to interpolate those to R (using RCall) to run a R’s `lm()`. I found and alternative using R’s `as.formula` to construct the formula from a string. But I was waiting Julia `Symbol`s to be interpolated to R `lm(data=$df, $(y) ~ $(x))` :

```nohighlight
R> fit <- lm(data=$df, actual_col ~ global_kl) # OK

R> fit <- lm(data=$df, $(y) ~ $(x)) # NOT OK
Error in model.frame.default(formula = `#JL`$y ~ `#JL`$x, data = `#JL`$df, : 
  object is not a matrix

R> fit <- lm(data=$df, $(string(y)) ~ $(string(x))) # NOT OK
Warning in model.response(mf, "numeric") : NAs introduced by coercion
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

R> fit <- lm(data=$df, as.formula($(string(y) * " ~ " * string(x)))) # Alternative

```

Is this behaviour correct?  
Best,

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [February 23, 2017, 9:20am UTC](https://discourse.julialang.org/t/creation-of-r-formula-with-interpolated-julia-symbol-rcall/2240/2 "2017-02-23T09:20:49Z")

</div>

I think this has to do with how R looks up variables specified in a formula. You can always do:

```julia
R> fit <- lm($(df[y]) ~ $(df[x]))

```
