I have a string vector shown below as Current output. I wonder if there a way to extract the unique elements of this vector (exclude "(Intercept)") to achieve my Desired output below?
Reproducible code:
dat <- transform(mtcars, vs=ifelse(vs==0,"y","n"))
m <- lm(mpg ~ cyl + hp + wt*vs+factor(gear), data = dat)
names(m$coef)
# Current output:
[1] "(Intercept)" "cyl" "hp" "wt"
[5] "vsy" "factor(gear)4" "factor(gear)5" "wt:vsy"
# Desired output:
[1] "cyl" "hp" "wt" "vs"
[5] "gear" "wt:vs"
>Solution :
We may get the term.labels attributes and extract the substring within the () if present
sub(".*\\(([^)]+)\\).*", "\\1",attr(terms(m), "term.labels"))
-output
[1] "cyl" "hp" "wt" "vs" "gear" "wt:vs"