R一对多Map

68de4m5k  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(114)

meth.genes是基于与meth Dataframe 的行名称对应的基因名称的字符向量。但是,它是一对多匹配,因此一个基因可以Map到meth中的多个行名称。经过一系列下游分析后,我将这些基因名称与另一组ID进行了匹配(Ensembl ID),现在我想将这些Ensembl ID与meth的行名称重新匹配,如果行名称重复,我想将增量值(例如,“.1”、“.2”)添加/粘贴到行名称。

meth.genes <- genes.mapped$nearestGeneSymbol
bm.meth <- getBM(attributes = c("ensembl_gene_id", "hgnc_symbol"),
                filters = "hgnc_symbol", mart = ensembl, values=meth.genes)
idx.meth <- meth.genes %>% match(table = bm.meth$hgnc_symbol)
meth.ensembl <- bm.meth$ensembl_gene_id[bm.meth$hgnc_symbol %in% meth.genes]
rownames(meth) <- meth.ensembl

追溯:

Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length

比如:

for i in length(duplicated(meth.ensembl)) {
    paste0(rownames(meth), ".", i)
}

数据:
rownames(meth)

> rownames(meth)[1:3500]
   [1] "cg00000029" "cg00000165" "cg00000236" "cg00000289" "cg00000292" "cg00000321"
   [7] "cg00000363" "cg00000622" "cg00000658" "cg00000721" "cg00000734" "cg00000769"
  [13] "cg00000905" "cg00000924" "cg00000948" "cg00000957" "cg00001245" "cg00001249"
  [19] "cg00001261" "cg00001349" "cg00001364" "cg00001446" "cg00001510" "cg00001582"
  [25] "cg00001583" "cg00001687" "cg00001747" "cg00001791" "cg00001809" "cg00001854"
  [31] "cg00001874" "cg00002033" "cg00002116" "cg00002145" "cg00002190" "cg00002224"
  [37] "cg00002236" "cg00002406" "cg00002426" "cg00002449" "cg00002464" "cg00002490"
  [43] "cg00002531" "cg00002591" "cg00002593" "cg00002597" "cg00002660" "cg00002719"
  [49] "cg00002769" "cg00002808" "cg00002809" "cg00002810" "cg00002837" "cg00003091"
  [55] "cg00003173" "cg00003181" "cg00003287" "cg00003345" "cg00003513" "cg00003529"
  [61] "cg00003578" "cg00003625" "cg00003784" "cg00003969" "cg00003994" "cg00004055"
  [67] "cg00004067" "cg00004072" "cg00004082" "cg00004089" "cg00004105" "cg00004121"
  [73] "cg00004192" "cg00004207" "cg00004209" "cg00004429" "cg00004533" "cg00004562"
  [79] "cg00004608" "cg00004773" "cg00004818" "cg00004883" "cg00004939" "cg00004963"
  [85] "cg00004979" "cg00004996" "cg00005010" "cg00005040" "cg00005072" "cg00005083"
  [91] "cg00005112" "cg00005166" "cg00005215" "cg00005297" "cg00005306" "cg00005390"
  [97] "cg00005437" "cg00005543" "cg00005617" "cg00005619" "cg00005740" "cg00005817"
 [103] "cg00005847" "cg00005849" "cg00005861" "cg00006032" "cg00006081" "cg00006122"
 [109] "cg00006198" "cg00006243" "cg00006397" "cg00006459" "cg00006626" "cg00006787"
 [115] "cg00006815" "cg00006884" "cg00006948" "cg00007032" "cg00007036" "cg00007064"
 [121] "cg00007076" "cg00007215" "cg00007221" "cg00007226" "cg00007239" "cg00007269"
 [127] "cg00007300" "cg00007324" "cg00007326" "cg00007420" "cg00007426" "cg00007466"
 [133] "cg00007644" "cg00007675" "cg00007981" "cg00007987" "cg00008004" "cg00008036"
 [139] "cg00008188" "cg00008345" "cg00008387" "cg00008446" "cg00008452" "cg00008488"
 [145] "cg00008493" "cg00008544" "cg00008564" "cg00008612" "cg00008629" "cg00008647"
 [151] "cg00008665" "cg00008671" "cg00008695" "cg00008713" "cg00008737" "cg00008810"
 [157] "cg00008823" "cg00008839" "cg00008932" "cg00008945" "cg00008971" "cg00009001"
 [163] "cg00009038" "cg00009053" "cg00009167" "cg00009196" "cg00009214" "cg00009293"
 [169] "cg00009364" "cg00009407" "cg00009412" "cg00009421" "cg00009553" "cg00009750"
 [175] "cg00009796" "cg00009871" "cg00009922" "cg00009944" "cg00009970" "cg00010046"
 [181] "cg00010078" "cg00010108" "cg00010168" "cg00010266" "cg00010445" "cg00010654"
 [187] "cg00010659" "cg00010742" "cg00010789" "cg00010853" "cg00010890" "cg00010946"
 [193] "cg00010947" "cg00010954" "cg00010992" "cg00011203" "cg00011225" "cg00011239"
 [199] "cg00011247" "cg00011284" "cg00011350" "cg00011459" "cg00011460" "cg00011482"
 [205] "cg00011513" "cg00011564" "cg00011578" "cg00011605" "cg00011616" "cg00011717"
 [211] "cg00011740" "cg00011856" "cg00011891" "cg00011924" "cg00012036" "cg00012126"
 [217] "cg00012148" "cg00012199" "cg00012203" "cg00012362" "cg00012386" "cg00012395"
 [223] "cg00012469" "cg00012522" "cg00012529" "cg00012638" "cg00012692" "cg00012698"
 [229] "cg00012701" "cg00012762" "cg00012792" "cg00012920" "cg00012992" "cg00013006"
 [235] "cg00013015" "cg00013196" "cg00013349" "cg00013374" "cg00013409" "cg00013410"
 [241] "cg00013441" "cg00013451" "cg00013475" "cg00013569" "cg00013618" "cg00013660"
 [247] "cg00013733" "cg00013899" "cg00013993" "cg00014085" "cg00014104" "cg00014112"
 [253] "cg00014118" "cg00014152" "cg00014168" "cg00014194" "cg00014260" "cg00014272"
 [259] "cg00014285" "cg00014289" "cg00014301" "cg00014333" "cg00014445" "cg00014484"
 [265] "cg00014667" "cg00014733" "cg00014752" "cg00014754" "cg00014786" "cg00014806"
 [271] "cg00014830" "cg00014900" "cg00014986" "cg00014996" "cg00014998" "cg00015024"
 [277] "cg00015058" "cg00015121" "cg00015261" "cg00015319" "cg00015340" "cg00015343"
 [283] "cg00015373" "cg00015530" "cg00015603" "cg00015639" "cg00015699" "cg00015770"
 [289] "cg00015930" "cg00016066" "cg00016127" "cg00016156" "cg00016223" "cg00016238"
 [295] "cg00016255" "cg00016324" "cg00016360" "cg00016406" "cg00016439" "cg00016481"
 [301] "cg00016498" "cg00016522" "cg00016593" "cg00016618" "cg00016718" "cg00016783"
 [307] "cg00016802" "cg00016814" "cg00016841" "cg00016892" "cg00016934" "cg00016968"
 [313] "cg00016996" "cg00017002" "cg00017033" "cg00017062" "cg00017121" "cg00017188"
 [319] "cg00017203" "cg00017221" "cg00017271" "cg00017437" "cg00017441" "cg00017461"
 [325] "cg00017475" "cg00017489" "cg00017601" "cg00017826" "cg00017842" "cg00017931"
 [331] "cg00017970" "cg00018024" "cg00018044" "cg00018128" "cg00018181" "cg00018184"
 [337] "cg00018198" "cg00018204" "cg00018229" "cg00018261" "cg00018524" "cg00018539"
 [343] "cg00018557" "cg00018606" "cg00018696" "cg00018739" "cg00018845" "cg00018850"
 [349] "cg00018896" "cg00019093" "cg00019118" "cg00019137" "cg00019151" "cg00019301"
 [355] "cg00019308" "cg00019366" "cg00019377" "cg00019458" "cg00019463" "cg00019495"
 [361] "cg00019511" "cg00019576" "cg00019678" "cg00019759" "cg00019778" "cg00019826"
 [367] "cg00019841" "cg00019877" "cg00019997" "cg00020052" "cg00020175" "cg00020191"
 [373] "cg00020297" "cg00020340" "cg00020348" "cg00020390" "cg00020419" "cg00020474"
 [379] "cg00020533" "cg00020622" "cg00020649" "cg00020720" "cg00020794" "cg00020799"
 [385] "cg00020991" "cg00021028" "cg00021030" "cg00021108" "cg00021128" "cg00021152"
 [391] "cg00021166" "cg00021237" "cg00021242" "cg00021256" "cg00021325" "cg00021476"
 [397] "cg00021513" "cg00021527" "cg00021539" "cg00021540" "cg00021550" "cg00021558"
 [403] "cg00021576" "cg00021783" "cg00021786" "cg00021933" "cg00021944" "cg00021945"
 [409] "cg00022024" "cg00022054" "cg00022076" "cg00022101" "cg00022138" "cg00022145"
 [415] "cg00022235" "cg00022259" "cg00022262" "cg00022308" "cg00022558" "cg00022594"
 [421] "cg00022633" "cg00022642" "cg00022688" "cg00022858" "cg00022866" "cg00022871"
 [427] "cg00022900" "cg00022911" "cg00022997" "cg00023001" "cg00023024" "cg00023132"
 [433] "cg00023154" "cg00023168" "cg00023174" "cg00023185" "cg00023233" "cg00023351"
 [439] "cg00023411" "cg00023415" "cg00023464" "cg00023492" "cg00023673" "cg00023695"
 [445] "cg00023750" "cg00023808" "cg00023919" "cg00024046" "cg00024086" "cg00024192"
 [451] "cg00024280" "cg00024382" "cg00024396" "cg00024404" "cg00024416" "cg00024471"
 [457] "cg00024472" "cg00024494" "cg00024722" "cg00024790" "cg00024812" "cg00024967"
 [463] "cg00024989" "cg00025044" "cg00025138" "cg00025211" "cg00025331" "cg00025347"
 [469] "cg00025357" "cg00025405" "cg00025436" "cg00025496" "cg00025591" "cg00025643"
 [475] "cg00025647" "cg00025715" "cg00025795" "cg00025823" "cg00025877" "cg00025981"
 [481] "cg00025991" "cg00026030" "cg00026033" "cg00026104" "cg00026119" "cg00026141"
 [487] "cg00026186" "cg00026222" "cg00026230" "cg00026290" "cg00026327" "cg00026362"
 [493] "cg00026375" "cg00026412" "cg00026455" "cg00026457" "cg00026546" "cg00026553"
 [499] "cg00026603" "cg00026703" "cg00026731" "cg00026776" "cg00026803" "cg00026853"
 [505] "cg00026857" "cg00026909" "cg00026919" "cg00026938" "cg00026974" "cg00027037"
 [511] "cg00027081" "cg00027083" "cg00027114" "cg00027120" "cg00027232" "cg00027400"
 [517] "cg00027466" "cg00027499" "cg00027570" "cg00027586" "cg00027637" "cg00027669"
 [523] "cg00027990" "cg00027995" "cg00028013" "cg00028014" "cg00028034" "cg00028135"
 [529] "cg00028184" "cg00028211" "cg00028299" "cg00028318" "cg00028336" "cg00028338"
 [535] "cg00028429" "cg00028431" "cg00028455" "cg00028487" "cg00028598" "cg00028636"
 [541] "cg00028678" "cg00028749" "cg00028757" "cg00028829" "cg00028844" "cg00028870"
 [547] "cg00028918" "cg00028929" "cg00028935" "cg00028993" "cg00029053" "cg00029161"
 [553] "cg00029233" "cg00029246" "cg00029282" "cg00029284" "cg00029353" "cg00029397"
 [559] "cg00029619" "cg00029640" "cg00029678" "cg00029821" "cg00029825" "cg00029826"
 [565] "cg00029831" "cg00029931" "cg00029979" "cg00030047" "cg00030125" "cg00030176"
 [571] "cg00030291" "cg00030420" "cg00030422" "cg00030423" "cg00030432" "cg00030476"
 [577] "cg00030501" "cg00030508" "cg00030523" "cg00030588" "cg00030627" "cg00030682"
 [583] "cg00030774" "cg00030795" "cg00030807" "cg00030816" "cg00031013" "cg00031063"
 [589] "cg00031105" "cg00031162" "cg00031165" "cg00031187" "cg00031256" "cg00031277"
 [595] "cg00031303" "cg00031340" "cg00031346" "cg00031402" "cg00031443" "cg00031456"
 [601] "cg00031476" "cg00031491" "cg00031556" "cg00031663" "cg00031708" "cg00031722"
 [607] "cg00031759" "cg00031779" "cg00031967" "cg00031973" "cg00032036" "cg00032192"
 [613] "cg00032205" "cg00032227" "cg00032246" "cg00032308" "cg00032366" "cg00032419"
 [619] "cg00032609" "cg00032610" "cg00032643" "cg00032664" "cg00032686" "cg00032701"
 [625] "cg00032756" "cg00032789" "cg00032805" "cg00032852" "cg00032879" "cg00032884"
 [631] "cg00032912" "cg00033127" "cg00033165" "cg00033220" "cg00033304" "cg00033334"
 [637] "cg00033371" "cg00033457" "cg00033490" "cg00033516" "cg00033533" "cg00033551"
 [643] "cg00033643" "cg00033666" "cg00033668" "cg00033703" "cg00033773" "cg00033806"
 [649] "cg00033834" "cg00033877" "cg00033915" "cg00034003" "cg00034019" "cg00034039"
 [655] "cg00034076" "cg00034101" "cg00034130" "cg00034336" "cg00034376" "cg00034407"
 [661] "cg00034416" "cg00034433" "cg00034468" "cg00034555" "cg00034556" "cg00034559"
 [667] "cg00034574" "cg00034611" "cg00034639" "cg00034755" "cg00034769" "cg00034852"
 [673] "cg00035000" "cg00035074" "cg00035197" "cg00035220" "cg00035237" "cg00035249"
 [679] "cg00035268" "cg00035316" "cg00035326" "cg00035398" "cg00035453" "cg00035623"
 [685] "cg00035630" "cg00035636" "cg00035673" "cg00035812" "cg00035923" "cg00035937"
 [691] "cg00035945" "cg00035969" "cg00035980" "cg00036011" "cg00036110" "cg00036119"
 [697] "cg00036137" "cg00036258" "cg00036263" "cg00036299" "cg00036324" "cg00036328"
 [703] "cg00036347" "cg00036386" "cg00036440" "cg00036570" "cg00036585" "cg00036588"
 [709] "cg00036599" "cg00036614" "cg00036664" "cg00036723" "cg00036732" "cg00036788"
 [715] "cg00036871" "cg00036976" "cg00037013" "cg00037056" "cg00037058" "cg00037072"
 [721] "cg00037117" "cg00037153" "cg00037186" "cg00037223" "cg00037314" "cg00037388"
 [727] "cg00037400" "cg00037406" "cg00037441" "cg00037450" "cg00037457" "cg00037681"
 [733] "cg00037763" "cg00037856" "cg00037930" "cg00037940" "cg00037952" "cg00038131"
 [739] "cg00038167" "cg00038233" "cg00038239" "cg00038342" "cg00038436" "cg00038446"
 [745] "cg00038675" "cg00038711" "cg00038736" "cg00038857" "cg00038877" "cg00039016"
 [751] "cg00039044" "cg00039070" "cg00039129" "cg00039147" "cg00039177" "cg00039326"
 [757] "cg00039385" "cg00039463" "cg00039478" "cg00039480" "cg00039489" "cg00039505"
 [763] "cg00039564" "cg00039627" "cg00039793" "cg00039794" "cg00039801" "cg00039864"
 [769] "cg00039960" "cg00040007" "cg00040008" "cg00040027" "cg00040047" "cg00040268"
 [775] "cg00040312" "cg00040322" "cg00040367" "cg00040380" "cg00040411" "cg00040426"
 [781] "cg00040427" "cg00040446" "cg00040455" "cg00040466" "cg00040566" "cg00040569"
 [787] "cg00040575" "cg00040588" "cg00040590" "cg00040861" "cg00040862" "cg00040873"
 [793] "cg00040923" "cg00040945" "cg00040986" "cg00041014" "cg00041047" "cg00041084"
 [799] "cg00041270" "cg00041328" "cg00041368" "cg00041401" "cg00041467" "cg00041575"
 [805] "cg00041599" "cg00041636" "cg00041666" "cg00041759" "cg00041865" "cg00041872"
 [811] "cg00041873" "cg00041898" "cg00041901" "cg00041989" "cg00042081" "cg00042144"
 [817] "cg00042186" "cg00042263" "cg00042325" "cg00042356" "cg00042409" "cg00042478"
 [823] "cg00042568" "cg00042611" "cg00042657" "cg00042837" "cg00042874" "cg00042882"
 [829] "cg00042914" "cg00043005" "cg00043043" "cg00043095" "cg00043284" "cg00043364"
 [835] "cg00043371" "cg00043379" "cg00043444" "cg00043510" "cg00043591" "cg00043599"
 [841] "cg00043788" "cg00043790" "cg00043800" "cg00043819" "cg00043949" "cg00044050"
 [847] "cg00044097" "cg00044170" "cg00044245" "cg00044289" "cg00044299" "cg00044318"
 [853] "cg00044372" "cg00044466" "cg00044505" "cg00044665" "cg00044796" "cg00044871"
 [859] "cg00044950" "cg00044995" "cg00045061" "cg00045114" "cg00045190" "cg00045275"
 [865] "cg00045303" "cg00045515" "cg00045532" "cg00045607" "cg00045689" "cg00045690"
 [871] "cg00045748" "cg00045753" "cg00045762" "cg00045787" "cg00045902" "cg00045910"
 [877] "cg00046018" "cg00046021" "cg00046026" "cg00046041" "cg00046099" "cg00046105"
 [883] "cg00046336" "cg00046499" "cg00046560" "cg00046623" "cg00046625" "cg00046744"
 [889] "cg00046815" "cg00046899" "cg00046913" "cg00046991" "cg00047050" "cg00047052"
 [895] "cg00047079" "cg00047162" "cg00047185" "cg00047287" "cg00047338" "cg00047469"
 [901] "cg00047501" "cg00047683" "cg00047694" "cg00047815" "cg00047844" "cg00048436"
 [907] "cg00048697" "cg00048743" "cg00048746" "cg00048759" "cg00048832" "cg00048838"
 [913] "cg00049033" "cg00049047" "cg00049102" "cg00049156" "cg00049253" "cg00049286"
 [919] "cg00049323" "cg00049330" "cg00049380" "cg00049382" "cg00049400" "cg00049411"
 [925] "cg00049440" "cg00049475" "cg00049502" "cg00049595" "cg00049616" "cg00049664"
 [931] "cg00049691" "cg00049709" "cg00049718" "cg00049729" "cg00049868" "cg00049883"
 [937] "cg00049894" "cg00049947" "cg00049973" "cg00050133" "cg00050152" "cg00050224"
 [943] "cg00050247" "cg00050271" "cg00050289" "cg00050294" "cg00050312" "cg00050402"
 [949] "cg00050482" "cg00050496" "cg00050590" "cg00050692" "cg00050792" "cg00050938"
 [955] "cg00050984" "cg00051068" "cg00051179" "cg00051307" "cg00051371" "cg00051483"
 [961] "cg00051546" "cg00051590" "cg00051662" "cg00051704" "cg00051782" "cg00051818"
 [967] "cg00051952" "cg00051979" "cg00052046" "cg00052246" "cg00052290" "cg00052385"
 [973] "cg00052422" "cg00052433" "cg00052550" "cg00052578" "cg00052588" "cg00052772"
 [979] "cg00052903" "cg00052964" "cg00053063" "cg00053073" "cg00053086" "cg00053135"
 [985] "cg00053261" "cg00053361" "cg00053373" "cg00053393" "cg00053507" "cg00053647"
 [991] "cg00053661" "cg00053728" "cg00053755" "cg00053870" "cg00053916" "cg00053927"
 [997] "cg00054173" "cg00054197" "cg00054210" "cg00054248"
 [ reached getOption("max.print") -- omitted 2500 entries ]

genes.mapped

> dput(genes.mapped[1:3,])
structure(list(queryHits = 1:3, subjectHits = c(17721L, 11282L, 
20626L), distance = c(237L, 11879L, 0L), nearestGeneSymbol = c("RBL2", 
"BARHL2", "VDAC3")), row.names = c("cg00000029", "cg00000165", 
"cg00000236"), class = "data.frame")

idx.meth

> dput(idx.meth[1:3])
c(3185L, 361L, 4196L)

meth.genes

> dput(meth.genes[1:3])
c("RBL2", "BARHL2", "BARHL2")

meth.ensembl
dput(方法总体[1:3])

c("ENSG00000175899", "ENSG00000184389", "ENSG00000184389")

预期产出:
row.names(meth)

c("ENSG00000175899", "ENSG00000184389.1", "ENSG00000184389.2")
irlmq6kh

irlmq6kh1#

我们可以使用ave来完成此操作

ave(meth.ensemble, meth.genes,
    FUN = function(z) if (length(z) == 1) z else paste(z, seq_along(z), sep = "."))
# [1] "ENSG00000175899"   "ENSG00000184389.1" "ENSG00000184389.2"

数据

meth.genes <- c("RBL2", "BARHL2", "BARHL2")   
meth.ensemble <- c("ENSG00000175899", "ENSG00000184389", "ENSG00000184389")
pkwftd7m

pkwftd7m2#

您可以在分组的框架内使用粘贴rowid() by meth.genes

library(data.table)

f <- \(e) if(length(e)>1) paste0(e,".", rowid(e)) else e

data.table(meth.genes, meth.ensemble)[,meth.ensemble:=f(meth.ensemble),meth.genes][]

输出:

meth.genes     meth.ensemble
1:       RBL2   ENSG00000175899
2:     BARHL2 ENSG00000184389.1
3:     BARHL2 ENSG00000184389.2

然后可以将其连接回meth

相关问题