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?
CodePudding user response:
Perhaps this helps
reformulate(setdiff(all.vars(form), c(exposure, outcome)))
~C D
CodePudding user response:
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"