从矢量创建一个命名列表而不复制/粘贴

时间:2015-11-30 06:57:35

标签: r list vector get

我有几个向量组合成一个命名列表(“my_list”)。向量的名称已经存储在向量中(“zI”)。

> zI
[1] "Chemokines"            "Cell_Cycle"            "Regulation"           
[4] "Senescence"            "B_cell_Functions"      "T_Cell_Functions"     
[7] "Cell_Functions"        "Adhesion"              "Transporter_Functions"
[10] "Complement"            "Pathogen_Defense"      "Cytokines"            
[13] "Antigen_Processing"    "Leukocyte_Functions"   "TNF_Superfamily"      
[16] "Macrophage_Functions"  "Microglial_Functions"  "Interleukins"         
[19] "Cytotoxicity"          "NK_Cell_Functions"     "TLR"       

如果它是少量向量,我只需要

my_list <- setNames(list(Chemokines, Adhesion), c("Chemokines", "Adhesion"))

我想找到一种更聪明的方法,除了将矢量名称组合成一个长字符串,然后复制/粘贴。

> toString(zI)
[1] "Chemokines, Cell_Cycle, Regulation, Senescence, B_cell_Functions, T_Cell_Functions, Cell_Functions, Adhesion, Transporter_Functions, Complement, Pathogen_Defense, Cytokines, Antigen_Processing, Leukocyte_Functions, TNF_Superfamily, Macrophage_Functions, Microglial_Functions, Interleukins, Cytotoxicity, NK_Cell_Functions, TLR"
> my_lists <- list(Chemokines, Cell_Cycle, Regulation, Senescence, B_cell_Functions, T_Cell_Functions, Cell_Functions, Adhesion, Transporter_Functions, Complement, Pathogen_Defense, Cytokines, Antigen_Processing, Leukocyte_Functions, TNF_Superfamily, Macrophage_Functions, Microglial_Functions, Interleukins, Cytotoxicity, NK_Cell_Functions, TLR)
> my_lists <- setNames(my_lists, zI)

这可能是一个非常基本的问题,但我已经搜索并阅读了10个单独的线程,但仍然无法弄明白。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我们可以使用mget来获取字符串的值。

mget(zI)