I have a regression formula string "y ~ x + z". How do I get "x" and "z" from it? I need to get the column names I use for regression..
I have a regression formula string "y ~ x + z". How do I get "x" and "z" from it? I need to get the column names I use for regression..
We can convert the string to formula object and get the variables with all.vars. The first element would be the dependent variable, so it is removed with indexing ([-1])
all.vars(as.formula(str1))[-1]
#[1] "x" "z"
Or as @ducymayr mentioned
attr(terms(as.formula(str1)), "term.labels")
#[1] "x" "z"
str1 <- "y ~ x + z"