简体   繁体   中英

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)

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"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM