将列表转换为R中的命名向量

时间:2016-03-18 20:40:16

标签: r pvclust

我使用R上的包pvclust进行了聚类分析。我使用命令“pvpick”提取了生成的聚类组件,得到了一个包含8个聚类的列表。

[[1]]
[1] "sco.plu" "tra.myo"

[[2]]
[1] "sya.pap" "syn.foe" "syn.int"

[[3]]
[1] "par.bra" "sya.mic"

[[4]]
[1] "bal.cap" "spa.axi"

[[5]]
[1] "achi.lin" "gin.cir"  "gym.vic"  "tha.nat" 

[[6]]
 [1] "alb.vul"  "car.bar"  "cha.fab"  "cyn.vor"  "eut.all"  "fis.tab"  "hae.aur"  "hae.ste"  "pol.vir"  "sel.cru"  "spa.fro" 
[12] "tri.letp" "ula.lef" 

[[7]]
 [1] "aux.roc" "car.cry" "car.hip" "car.lat" "clo.chr" "cyn.jam" "ech.nau" "ech.neu" "elo.sau" "hae.par" "hae.plu" "lut.syn"
[13] "lyc.bat" "ocy.chr" "oli.pal" "opi.ogl" "ort.rub" "rac.can" "rhi.por" "sco.bra" "sco.cav" "sco.reg"

[[8]]
 [1] "aca.qua" "aca.bah" "aca.chi" "alu.mon" "ani.vir" "arc.rho" "asp.lun" "bag.bag" "bag.mar" "cal.cal" "cal.pen" "cal.pnt"
[13] "can.pul" "cat.spi" "cen.par" "cha.str" "chi.spi" "con.nob" "cyn.lei" "cyn.mic" "dac.vol" "dec.pun" "dia.aur" "epi.ads"
[25] "gen.lut" "gen.mac" "het.cru" "hol.cil" "hol.ads" "lac.tri" "lar.bre" "lut.ana" "lut.joc" "mic.fur" "not.gra" "pri.are"
[37] "pri.pun" "sci.pro" "sel.vom" "sel.bro"

对于我的下一个分析,我需要从这个列表中创建一个“命名向量”,就像你使用群集包的“cuttree”函数时得到的那个,每个物种名称(即“sco.plu”)作为在向量中具有对应簇号的名称。这看起来像这样:

> memb_average
 aca.qua  aca.bah  aca.chi achi.lin  alb.vul  alu.mon  ani.vir  arc.rho  asp.lun  aux.roc  bag.bag  bag.mar  bal.cap  cal.cal 
       1        1        1        2        3        1        1        1        1        4        1        1        1        1 
 cal.pen  cal.pnt  can.pul  car.bar  car.cry  car.hip  car.lat  cat.spi  cen.par  cha.fab  cha.str  chi.spi  clo.chr  con.nob 
       1        1        1        3        4        4        4        1        1        4        1        1        4        1 
 cyn.jam  cyn.lei  cyn.mic  cyn.vor  dac.vol  dec.pun  dia.aur  ech.nau  ech.neu  elo.sau  epi.ads  eug.bra  eut.all  fis.tab 
       5        6        6        3        5        1        1        4        4        5        1        1        4        3 
 gen.lut  gen.mac  gin.cir  gym.vic  hae.aur  hae.par  hae.plu  hae.ste  het.cru  hol.cil  hol.ads  lac.tri  lar.bre  lut.ana 
       6        6        7        2        4        4        4        4        2        1        1        1        6        1 
 lut.joc  lut.syn  lyc.bat  mic.fur  not.gra  ocy.chr  oli.pal  opi.ogl  ort.rub  par.bra  pol.vir  pri.are  pri.pun  rac.can 
       1        4        4        1        6        4        5        4        5        1        6        1        5        4 
 rhi.por  sci.pro  sco.bra  sco.cav  sco.reg  sco.plu  sel.cru  sel.vom  sel.bro  spa.axi  spa.fro  sya.mic  sya.pap  syn.foe 
       4        4        4        4        4        2        4        5        5        3        3        1        2        2 
 syn.int  tha.nat  tra.myo tri.letp  ula.lef 
   2        2        2        4        3 

但我真的很难找到一种方法,并再次感觉它可能有一个非常简单和优雅的解决方案。

1 个答案:

答案 0 :(得分:1)

示例清单:

x = list(c("a", "b"), "c", c("d", "e", "f"))

## make vector
y = rep(seq_along(x), times = sapply(x, length))
## name vector
names(y) = unlist(x)
## verify result
y
# a b c d e f 
# 1 1 2 3 3 3