rpvclust

Converting a list into a named vector in R


I did a cluster analysis with the package pvclust on R. I extracted the resultant clusters components with the command "pvpick" resulting in a list with 8 clusters.

[[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"

For my next analysis I need to make a "named vector" from this list just like the one you get when you use the "cuttree" function of the cluster package, with each species name (i.e "sco.plu") as a name with its correspondent cluster number in the vector. That would look like this:

> 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 

but I´m really struggling to find a way to do so and have again that feeling that it might have a very simple and elegant solution.


Solution

  • Sample list:

    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