[SOLVED] extracting unique elements of a string vector


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)


# 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"


We may get the term.labels attributes and extract the substring within the () if present

sub("[^:]+\\(([^)]+).*", "\\1", attr(terms(m), "term.labels"))


[1] "cyl"   "hp"    "wt"    "vs"    "gear"  "wt:vs"

For the updated model

m2 <- lm(mpg ~ cyl + hp + wt*vs*factor(gear)+0, data = dat)
sub("[^:]+\\(([^)]+).*", "\\1", attr(terms(m2), "term.labels"))
[1] "cyl"        "hp"         "wt"         "vs"         "gear"  
[6]     "wt:vs"      "wt:gear"    "vs:gear"    "wt:vs:gear"

Answered By – akrun

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *