Home > front end >  extracting unique elements of a string vector
extracting unique elements of a string vector

Time:03-25

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"

CodePudding user response:

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"

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"
  • Related