Get the right hand side variables of an R formula
If you have a function in your formula, e.g., log
, and want to subset the data frame based on the variables, you can use get_all_vars
. This will ignore the function and extract the untransformed variables:
f2 <- Species ~ log(Petal.Length) + Petal.Widthget_all_vars(f2[-2], iris) Petal.Length Petal.Width1 1.4 0.22 1.4 0.23 1.3 0.24 1.5 0.2...
If you just want the variable names, all.vars
is a very helpful function:
all.vars(f2[-2])[1] "Petal.Length" "Petal.Width"
The [-2]
is used to exclude the left hand side.