在R中搜索层次结构列表

时间:2018-10-30 20:57:48

标签: r data-management

var_label包中,由labelled函数产生的变量标签列表很长,如下所示:

list(prtclcie = "Which party feel closer to, Ireland", dweight = "Design weight", 
    edlvfdit = "Father's highest level of education, Italy", 
    edlvpdru = "Partner's highest level of education, Russian Federation", 
    edlvdpt = "Highest level of education, Portugal", icwhct = "Have a set 'basic' or contracted number of hours", 
    uemp3m = "Ever unemployed and seeking work for a period more than three months", 
    icomdng = "Interviewer code, one/more than one doing last 7 days", 
    edlvfenl = "Father's highest level of education, Netherlands", 
    prtvtdfi = "Party voted for in last national election, Finland")

我想找到一个基于关键字搜索此列表的命令,其方式是输出为列名stg。像prtclcieedlvfdit之类。

我尝试使用grep("keyword", list),但这只给出了“命中”的行号,而不是列表结构中的上层名称。

欢迎任何想法!谢谢!

1 个答案:

答案 0 :(得分:1)

使用value=TRUE返回值,而不仅仅是返回索引

grep("educ", list, value=TRUE)

如果只需要列表中的名称,请使用

names(grep("educ", list, value=TRUE))