Sebastian Gerdes
Sebastian Gerdes

Reputation: 166

Constructing a formula - good coding style

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

Answers (1)

Ma&#235;l
Ma&#235;l

Reputation: 51974

You can use reformulate:

reformulate("x", "y")
#y ~ x

reformulate(c("x", "z"), "y")
#y ~ x + z

Upvotes: 1

Related Questions