Reputation: 344
I often need to convert large sets of variables from data frames into formulaic expressions. These variables sometimes have dashes/hyphens in them, and this gives as.formula
trouble.
I am aware that I could just gsub
these out of the data frame names, but that's undesirable as I need to map back to other annotation files which use the hyphens.
I have one workaround that leverages backticks (`
) and paste0
below, but it feels clunky (i.e. is hard to read and may be less efficient than something in base that I've missed).
Is there a simpler expression that will get this done?
dat <- data.frame(rnorm(100), rnorm(100))
names(dat) <- c("y", "x-1")
form <- as.formula(paste("y~", names(dat)[2]))
form #treats hyphen as minus
lm(form, data = dat) #error
# is this the only workaround?
form <- as.formula(paste("y~", paste0("`", names(dat)[2], "`")))
form
lm(form, data = dat)
Upvotes: 2
Views: 818
Reputation: 132736
I'd use .
:
f <- y ~ .
lm(f, dat)
Subset the data.frame to only contain variables of interest.
Upvotes: 3