提取字符串向量的唯一元素

extracting unique elements of a string vector

我有一个字符串向量,如下所示 Current output。我想知道是否有办法提取此向量的唯一元素(不包括"(Intercept)")以实现我下面的Desired output

可重现代码:

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"

我们可以获取 term.labels 属性并提取 () 中的子字符串(如果存在)

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

-输出

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

更新模型

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"