Reputation: 1897
exposure <- 'B'
outcome <- 'A'
I have a formula that looks like this in R
form <- formula(A ~ B + C + D)
I want to convert it to a string like this where I remove the exposure and the outcome
C + D
How can i do this in R?
Upvotes: 0
Views: 43
Reputation: 887901
Perhaps this helps
reformulate(setdiff(all.vars(form), c(exposure, outcome)))
~C + D
Upvotes: 1
Reputation: 360
form_str <- as.character(form)
[1] "~" "A" "B + C + D"
variables <- strsplit(form_str[3], " + ", fixed = TRUE)[[1]]
[1] "B" "C" "D"
paste(setdiff(variables, exposure), collapse = " + ")
"C + D"
Upvotes: 0