Reputation: 166
this is more a question of good coding style.
I have a piece of code that works, but I am wondering if there is a less "hacky" version that works more directly.
Here is what I am doing:
var_name <- 'x'
my_formula <- paste('y ~ ', var_name)
as.formula(my_formula)
Any advice is appreciated!
Thanks a lot and best greetings, Sebastian
Upvotes: 1
Views: 53
Reputation: 51974
You can use reformulate
:
reformulate("x", "y")
#y ~ x
reformulate(c("x", "z"), "y")
#y ~ x + z
Upvotes: 1