1

I have a list of data frames in R. I would like to group them base on column names and plot the columns in separate plots. As sample, I just put the first 3 data frame :

     samp = list(`YUN_p07-i01_ClusterC2_ILA1` = structure(list(DLC12s = c(86.1999808927684, 
83.1661021183023, 83.1630245356896, 82.629186143684, 76.4255917126618, 
69.0475018007389, 66.6545180907876, 66.8711770634756, 68.8253794872399, 
68.7704831147614, 70.26250005327, 72.2278229651729, 74.4860838632999, 
77.1187068050456, 80.2289835630146, 83.5132828998463, 86.932579025573, 
90.0798627067643, 93.3213802621344, 96.3445845139445, 98.3384894630155, 
99.0003993047245), DLC17p = c(0.518445241686927, 1.50124229711346, 
1.53666803655824, 1.6104774860386, 2.18776070287186, 2.89527306653092, 
3.02632630555093, 3.02084155608756, 2.96675286121521, 2.96824532869834, 
2.92374876221941, 2.85102738508049, 2.74281718710316, 2.59153531461665, 
2.38055370716171, 2.11978801815047, 1.82682897488416, 1.52188989540395, 
1.17488046778467, 0.808365945518941, 0.503029623152029, 0.344992864013846
), DLC17q = c(0.29298751417293, 0.310534586369366, 0.310975265273449, 
0.308964093096275, 0.282724685666571, 0.243827177391463, 0.227830429978927, 
0.228345448650973, 0.241893146895052, 0.241682720609163, 0.252859420301516, 
0.266098094633695, 0.281076406680632, 0.299369324987808, 0.314254088328931, 
0.3279481386935, 0.340224557772322, 0.346805620741348, 0.350247337108639, 
0.351854889611391, 0.35031474482401, 0.348995199337251), DLC21gs = c(12.7175188616849, 
0.154406878557904, 0.159731563104607, 0.169321198108756, 0.267378128318265, 
0.397419705772557, 0.44521252330632, 0.452728605131062, 0.443770156726482, 
0.444584968615046, 0.434755098793551, 0.417971278512881, 0.395336173384056, 
0.368196278150562, 0.330177882848121, 0.288032267856803, 0.239930076244037, 
0.188873677400258, 0.129978946755775, 0.0701514631712911, 0.0251342731781843, 
0.00695036506137349), DLC24as = c(0.222826419796297, 0.241154083007526, 
0.242223080477684, 0.240731675193117, 0.226596554972244, 0.21240885403336, 
0.208095190949987, 0.21023410948102, 0.214507151215669, 0.214299097883211, 
0.216900688304365, 0.219479478428492, 0.224000630495338, 0.229218509216246, 
0.231508421326128, 0.234347481359479, 0.23588141958107, 0.236852522349953, 
0.235824289054205, 0.232912854777051, 0.228814319095028, 0.22741553913916
), DLC24bs = c(0.0277354272216626, 0.0417048470046467, 0.0411536057135019, 
0.0402770009471399, 0.0354691302004106, 0.0295040606839387, 0.028110042042609, 
0.0284524582542177, 0.0290303255956694, 0.0290063120570954, 0.0297072945388072, 
0.0301863940784852, 0.0307427943332238, 0.0314336800532286, 0.0321251759995036, 
0.0326178003245064, 0.0329001343436253, 0.0331890892391769, 0.0331135271129324, 
0.0327367223516411, 0.0321472514372787, 0.0315954058283562), 
    DLC31s = c(0.00179953374020422, 0.451788608573938, 0.442934432171455, 
    0.445622904440189, 0.436628281561018, 0.462579527770983, 
    0.510305042575961, 0.519610708797345, 0.498945044006626, 
    0.499611904183969, 0.482726079985608, 0.464696671845943, 
    0.444733898029508, 0.398150879001437, 0.352585611190157, 
    0.300299230995862, 0.250924147893593, 0.190372282644939, 
    0.132022999917796, 0.0682184829199272, 0.0248233408710608, 
    0.00694358215874919), DLC41es = c(0.000328145524310888, 0.00948175203382567, 
    0.00948726802358231, 0.00980032297688727, 0.0130918290429131, 
    0.0174977088332496, 0.0198021150412779, 0.0212155979227139, 
    0.0206450767247023, 0.0206726687032939, 0.0198417905459509, 
    0.0189240645299554, 0.0174068418749516, 0.0163555696797261, 
    0.0141781715845917, 0.0123311285214278, 0.0101624804251044, 
    0.0076012046350463, 0.00519505761737627, 0.00259807980889207, 
    0.000800681858654099, 0.000118165044984858), DLC41is = c(0.00114419563486984, 
    0.369375491856062, 0.365756142769149, 0.365996996068891, 
    0.43473253600147, 0.522486021253744, 0.566638554193928, 0.576048885109279, 
    0.545417828999124, 0.546148065430032, 0.520960004519625, 
    0.492548164103139, 0.444546226248173, 0.40764142578374, 0.354307810337745, 
    0.30349340919296, 0.230361604467185, 0.176334548654245, 0.109242958959919, 
    0.0548975677897042, 0.0164264438346333, 0.00246476685547599
    ), DLC41ms = c(0.0031633859810912, 0.121520954862548, 0.123617669347512, 
    0.129348870659286, 0.185113267199568, 0.260688273927753, 
    0.293583723552447, 0.297108478910199, 0.284979540913941, 
    0.285360416955637, 0.277273203197587, 0.265924489053568, 
    0.243859111154412, 0.229095516920215, 0.193723860107464, 
    0.169379206447382, 0.135389835451235, 0.107304402977301, 
    0.0753978064138843, 0.0389266842035347, 0.013348618834338, 
    0.00335417641912744), DLC64h = c(0.00343783271186764, 1.06349409996102, 
    1.04667922660965, 1.05739737426366, 1.2009737414251, 1.34198171391244, 
    1.41037653788597, 1.39843919377863, 1.31487869047025, 1.31707204759337, 
    1.24794982386622, 1.16244825009117, 1.05998107861483, 0.945539545735093, 
    0.810207490164157, 0.670470450823383, 0.52527828693085, 0.393307678470125, 
    0.253483138003207, 0.125592625134136, 0.0383912764363229, 
    0.00642030766889736), DLC64l = c(2.45692655312564e-16, 1.90268291100716, 
    1.90734606271009, 2.00968790284476, 3.15314431238807, 4.79594454391171, 
    5.29316146375101, 5.24590709808705, 4.85531024823933, 4.86232591566839, 
    4.5896820926463, 4.22177579070747, 3.85622595630147, 3.43124349080794, 
    2.92657229461866, 2.38158734440488, 1.82091534585416, 1.32811381255006, 
    0.834033610589599, 0.368749633593734, 0.0810811404823062, 
    0.00316209058271388), DLC72 = c(0.010632549076346, 10.6665113713504, 
    10.6504031115509, 10.9831880316785, 15.1507951176905, 19.7728875452393, 
    21.3160399803826, 21.129890796314, 19.7584904417584, 19.8005074388411, 
    18.7410956878111, 17.3610969737621, 15.7731898324797, 13.9335136600018, 
    11.8308219233181, 9.64642262338299, 7.41862411058014, 5.38949255816869, 
    3.34519959854764, 1.50041053717485, 0.347198822980415, 0.0171882331657344
    ), h = c(-52.5, -43.375, -34.25, -25.125, -16, -6.375, 3.25, 
    12.875, 22.5, 22.5, 29.467, 36.433, 43.4, 51.1, 58.8, 66.5, 
    74.2, 81.4, 88.6, 95.8, 103, 110.2)), class = "data.frame", row.names = c(NA, 
-22L)), `SAM_p01-i01_B45-03` = structure(list(DLC12 = c(86.5314866162036, 
70.6482044279903, 70.0517266430886, 69.1537430043123, 67.8241590098885, 
66.7921442938182, 65.5937980140176, 64.3261418720238, 62.8913534820884, 
60.2489665221705, 56.6753714303144, 53.4520540590501, 50.3068923447928, 
48.3126391408391, 45.7382293718082, 44.108630511863, 43.321486762861, 
43.0602315026524, 43.0111480492834, 42.9379605954284, 42.9223167319234, 
42.8396978977298, 42.7920754385132, 42.7049422833556, 42.7031174954913, 
42.7102891219321, 42.6748905084158, 42.7650978277989, 43.0372974233205, 
43.4160213230891, 44.0275002603808, 44.9961254558944, 46.4686932865709, 
49.2809912275468, 55.0431218863332, 67.5331329321333, 88.8867737872429, 
99.4150369800821), DLC24a = c(0.218766367916309, 0.153340002337096, 
0.151650969728312, 0.149187928295569, 0.145890364320125, 0.143446126578526, 
0.140964905342863, 0.13812392978371, 0.135383865353581, 0.129413735907879, 
0.121387699080614, 0.113569036243044, 0.105845095969524, 0.100510612892751, 
0.0936069324743029, 0.0894943486944327, 0.0876085925358724, 0.0870050065625371, 
0.0869819496723875, 0.0868380015765139, 0.0868212695787194, 0.0866958376348893, 
0.086645708728405, 0.0865151999281387, 0.0865135517201478, 0.086534186426046, 
0.0865103663171702, 0.0869378045250614, 0.0879836961420589, 0.0894183411748553, 
0.0913138598157845, 0.0945822436516908, 0.0995456234554365, 0.109125877997524, 
0.127380884153403, 0.165166562786197, 0.224581307428501, 0.251060887424037
), DLC24b = c(0.0453214145449118, 0.0485214988383027, 0.0474234852066369, 
0.0459975660528709, 0.0439642773383507, 0.0424344466295988, 0.0406291833428262, 
0.0385910882375923, 0.0362724179811376, 0.0321579400348675, 0.0268641424420976, 
0.0224727618666185, 0.0186253035799354, 0.016430726972427, 0.0137797008874603, 
0.0122595872705079, 0.0116178253111307, 0.0114085223571877, 0.0113579355162143, 
0.0113600872937264, 0.0113676459438673, 0.0113948338535472, 0.011409838002603, 
0.0114454746305702, 0.011450626850776, 0.011457147558928, 0.0115163765216727, 
0.0116571243037127, 0.0118928895947969, 0.0122033597385413, 0.012615206546191, 
0.0132434489398068, 0.0142137755219769, 0.016076497250941, 0.019648872676804, 
0.0273543945082651, 0.0403086813652211, 0.0469900438394219), 
    DLC31 = c(0.114275756371283, 0.419696415865509, 0.418166663471472, 
    0.41659770277584, 0.412075330325881, 0.406296316875646, 0.404865668230333, 
    0.40199122147391, 0.397664754098204, 0.384758065118196, 0.359611489738715, 
    0.337066381557038, 0.31049067851873, 0.296809210627832, 0.277196869667497, 
    0.25971047000425, 0.249450511536022, 0.249950166945168, 0.249844869268875, 
    0.250428445647417, 0.251203765820931, 0.251349743180014, 
    0.252974109262994, 0.254301841336695, 0.2542961137628, 0.254257827350519, 
    0.254994504577032, 0.25585846552889, 0.257852426051227, 0.256590902847481, 
    0.255201118353802, 0.253687786597228, 0.249522336846397, 
    0.243218992806048, 0.221457233295584, 0.173167673208189, 
    0.0745166521363018, 0.0118927335597613), DLC41e = c(0.00112022892793795, 
    0.00529857259283213, 0.00513886656639065, 0.00513577550831923, 
    0.00498318974912717, 0.00480864175452075, 0.00470534905551177, 
    0.00454405919690493, 0.00433509316194446, 0.00405282002645228, 
    0.00364043874229719, 0.0034119775767327, 0.00328861599936114, 
    0.00312180175858676, 0.00310300107191918, 0.00313168682770946, 
    0.00308698623499945, 0.00313006479484543, 0.0031106433039962, 
    0.0031909369600748, 0.00319303983239588, 0.00319862778736807, 
    0.00325414565866252, 0.00329720389330231, 0.00329712683247052, 
    0.00329662311976585, 0.00338449846497849, 0.00342986707917998, 
    0.00344435269388162, 0.00355370257450106, 0.00356588396586719, 
    0.00357608951969794, 0.00364849556003587, 0.00365630346193987, 
    0.00353623622574113, 0.00306495528827925, 0.00187087677534341, 
    0.000767246364903308), DLC41i = c(0.138464782241703, 0.695771145407792, 
    0.678656847723093, 0.652820987658641, 0.614220855768878, 
    0.605164592690514, 0.589061464097191, 0.572828114303108, 
    0.558357480317725, 0.535805938686013, 0.503462966788739, 
    0.487773144497363, 0.467908482509039, 0.460694215981115, 
    0.45963949544064, 0.461477965786566, 0.469472540586466, 0.474132286796177, 
    0.475879264138292, 0.476130032032328, 0.476039141738809, 
    0.475900902572461, 0.476477622436718, 0.476544187858097, 
    0.476533064150817, 0.476514638318387, 0.478818676742515, 
    0.478351963263727, 0.476193805833404, 0.476028888774189, 
    0.472995262719091, 0.466040255253569, 0.455126942007137, 
    0.434475457645546, 0.390287233376125, 0.288899027555097, 
    0.106243915751312, 0.0059031597088719), DLC41m = c(0.02624806797749, 
    0.0910572322126005, 0.0910772065561211, 0.0910571646406683, 
    0.0922660406795948, 0.0938282326027338, 0.0930432613754086, 
    0.0942342368535042, 0.0950671623148232, 0.0968565540355199, 
    0.0991628672292148, 0.103129849170706, 0.10540808863291, 
    0.108077335190526, 0.111182242896488, 0.113356473143196, 
    0.117075597943824, 0.11764332028936, 0.117562270295499, 0.117469653538374, 
    0.117521497121909, 0.118157643858692, 0.120071400406781, 
    0.120418331734363, 0.120415520762977, 0.120397110727542, 
    0.121352538681384, 0.123711614322062, 0.123748714156593, 
    0.123947589454718, 0.124771697159254, 0.123833238508253, 
    0.121609278447632, 0.118496507991892, 0.108596794326569, 
    0.0844565328933596, 0.037930699698903, 0.0118228722878775
    ), DLC64h = c(0.0182870979585783, 0.0839347127169774, 0.0820496752995642, 
    0.0796857104239288, 0.076636326694896, 0.0740981166600861, 
    0.0721110662241128, 0.0702821912443217, 0.068262891201914, 
    0.0641913719951261, 0.0596382546252066, 0.055788498958326, 
    0.0524256485857287, 0.0506570165684668, 0.0488086020039789, 
    0.0479032070902036, 0.0472705352464485, 0.0472208188406132, 
    0.0469824183154683, 0.047018264107552, 0.0470103357329518, 
    0.0470375440907054, 0.0470562310613325, 0.0470744546889209, 
    0.0470743166442109, 0.0470768477898976, 0.0470842802403169, 
    0.0469135562298176, 0.0468468543689422, 0.0467067657278952, 
    0.0463100258520136, 0.0455636924415753, 0.0445267949801028, 
    0.0426557188239823, 0.038274989651877, 0.0283840679055483, 
    0.010545582038612, 0.000509047901546849), DLC64l = c(0.0775171406532654, 
    0.136702540728187, 0.142469508908647, 0.149231381067624, 
    0.161133814696488, 0.167862427276489, 0.17885757493889, 0.188322549460057, 
    0.202204943826697, 0.224182401507779, 0.254595240391027, 
    0.286367621990186, 0.321106527467607, 0.343385548016078, 
    0.368605928920641, 0.385543414984002, 0.396247373648929, 
    0.40012566445517, 0.401342670672085, 0.402437063371678, 0.402637227970377, 
    0.403164610194465, 0.403551159543014, 0.403880956940561, 
    0.403983023240631, 0.403923084291875, 0.404867616058887, 
    0.403980604314669, 0.402569298127318, 0.400532852311185, 
    0.397459089076149, 0.391746603729568, 0.381778845419467, 
    0.363069910185862, 0.322814659309606, 0.234950590644019, 
    0.0814069596737419, 0.00231177596624174), DLC72 = c(12.8285125272045, 
    27.7174734513101, 28.3316401334511, 29.256542779264, 30.6246707905386, 
    31.6699168051135, 32.8819635133752, 34.1649407374231, 35.6110979096556, 
    38.2796146505173, 41.8962654706474, 45.1383666690901, 48.3080092139444, 
    50.3076743911529, 52.8858478548286, 54.5184923343358, 55.2966832740955, 
    55.5491526463063, 55.5957899295336, 55.667166920044, 55.6818893443367, 
    55.7634023590981, 55.8064843463865, 55.8915800656335, 55.893319160544, 
    55.8862534124851, 55.9165806339803, 55.8240611726336, 55.5521705397113, 
    55.1749962743074, 54.5682675961309, 53.6116011854643, 52.161334621191, 
    49.3882335062894, 43.724881210651, 31.4614232630774, 10.535821537889, 
    0.253705252865466), h = c(-69.3, -65, -60, -55, -50, -47.5, 
    -45, -42.5, -39.9, -35.7, -30, -25, -20, -16.6, -11.5, -7, 
    -3.5, 0, 2.5, 6.5, 7.5, 11.5, 15, 21, 21.2, 21.2, 28.167, 
    35.133, 42.1, 48.26, 54.42, 60.58, 66.74, 73.94, 81.14, 88.34, 
    95.54, 102.74)), class = "data.frame", row.names = c(NA, 
-38L)), `NnG_p24-i02_c02_6P_M36` = structure(list(DLC12 = c(54.4435251593895, 
60.4058230578194, 58.6303989612682, 55.314337253841, 50.7710434064941, 
46.4037703430297, 42.6359255249323, 40.3038329424453, 39.8819638103591, 
39.5582413841129, 39.2469336458061, 39.2393700712619, 38.9400585523699, 
38.8578597755466, 39.1476476831477, 39.8302872496507, 41.0591799377689, 
42.9639100534251, 45.8265164643939, 50.0114270438487, 57.5147424189172, 
71.1293758377103, 89.5087392552777, 97.8345822443614), DLC24a = c(0.159886150336981, 
0.171677716391787, 0.166377841964712, 0.156878943554993, 0.144906495222232, 
0.132883089000661, 0.122449254454408, 0.116110146641472, 0.115259375778084, 
0.114175047208234, 0.113332296094903, 0.113304603571124, 0.112510701057897, 
0.112551069962873, 0.113587724778825, 0.115526596882998, 0.11894812242382, 
0.124318407579698, 0.13248316606602, 0.143755537700758, 0.16258338643667, 
0.195528593960096, 0.235012447394, 0.249957876295005), DLC24b = c(0.0184136799547997, 
0.0292407180672802, 0.0281028927262214, 0.0255667376929854, 0.0215804758294541, 
0.017381680478672, 0.0140059337827739, 0.0118466008198282, 0.011287638730048, 
0.0112765092353713, 0.0112329422618099, 0.0112330297626183, 0.011237752427963, 
0.0112795563693597, 0.0113607823334111, 0.0115973921926224, 0.0119648096088683, 
0.012380757457651, 0.0131655540718474, 0.0141270419507646, 0.015891534764751, 
0.0188195794912396, 0.021878775274657, 0.0223137535322092), DLC31 = c(0.105191462635085, 
0.376068325580247, 0.376822148945267, 0.368943653977019, 0.33636158761232, 
0.284947878504001, 0.2400420288855, 0.20608995012619, 0.203680811762795, 
0.204922698290645, 0.206917758925312, 0.206942866445647, 0.208514117710556, 
0.213361180672979, 0.212624193011083, 0.210194540549773, 0.207716791456246, 
0.202430582734739, 0.19696461613771, 0.18522171342043, 0.162299056496549, 
0.118762287053893, 0.0497061292818597, 0.00895304495111956), 
    DLC41e = c(0.00199259615219407, 0.00993957948672134, 0.0101554562378574, 
    0.0101560159924302, 0.0106651651113587, 0.0104559651129055, 
    0.0106576811811128, 0.0108636812239941, 0.0110594342077646, 
    0.0110975125031564, 0.01114685873109, 0.0111482116879716, 
    0.0114046785560539, 0.0115137687759619, 0.0115393723104415, 
    0.0114074345887843, 0.0113445432723503, 0.0111061333243449, 
    0.0106024728241209, 0.009886717682106, 0.00858079186869025, 
    0.00619948404416043, 0.0024922245932218, 0.000480232404315263
    ), DLC41i = c(0.0763052952592319, 0.480698795794534, 0.484636373649032, 
    0.488443139980859, 0.484223512236868, 0.45008230602985, 0.416449313587552, 
    0.389629202260568, 0.375495512850925, 0.377397517190444, 
    0.381124504812132, 0.381170763638473, 0.383605301298594, 
    0.385283553119306, 0.383183928045964, 0.380587068011987, 
    0.372613918151571, 0.360618300981876, 0.342602503938473, 
    0.317236790747977, 0.27105301415066, 0.182777789727521, 0.0617505616005118, 
    0.00307815038496876), DLC41m = c(0.108423763069501, 0.213642296571627, 
    0.224021470123322, 0.235703458986629, 0.247437705979371, 
    0.255629062531108, 0.260051914144357, 0.263430377810683, 
    0.264315432068389, 0.265150461368426, 0.265787300849609, 
    0.265818825867519, 0.269749171357754, 0.270860446221749, 
    0.269684829550434, 0.267126140943506, 0.263919871046971, 
    0.256284443998711, 0.245964175707705, 0.22687385585044, 0.19454871946828, 
    0.133912779773782, 0.049586430015697, 0.0104269884192644), 
    DLC64h = c(0.0682039035949202, 0.383871502474758, 0.388491367390502, 
    0.395755233673989, 0.393883110591027, 0.38357754392518, 0.371192627641175, 
    0.35879623584899, 0.352773981770983, 0.354202342501633, 0.356355111673662, 
    0.356394798433093, 0.358206787817173, 0.358829611133505, 
    0.356503939797175, 0.352564074882189, 0.344411784577679, 
    0.33324366230195, 0.315954338887504, 0.291798989347465, 0.248258599757461, 
    0.167373696825949, 0.0562840185760291, 0.00259384825815647
    ), DLC64l = c(0.0491324764617038, 0.000646464542124482, 0.000667879850640206, 
    0.000701790359459709, 0.000841724333258625, 0.00114200742199224, 
    0.00131740059947369, 0.00136310896030365, 0.00144901162200778, 
    0.00145565011406664, 0.00145811522861758, 0.00145829151877925, 
    0.00147135248013787, 0.00147067631008413, 0.00146381996555089, 
    0.00144440003600201, 0.00141179438808945, 0.00136721892293532, 
    0.00130176688585897, 0.00120290010563353, 0.00102531633992181, 
    0.000721485552802989, 0.000232649267060849, 1.06783775639768e-05
    ), DLC72 = c(8.336495144341, 36.1745297466092, 38.0024519770216, 
    41.4219739594744, 46.169603890161, 50.8277951216586, 54.8483955416169, 
    57.3059654863845, 57.6667222086439, 57.9424246003873, 58.1910628562599, 
    58.1986690819518, 58.4265648533366, 58.4351633910753, 58.0802412959206, 
    57.3306586991926, 56.0356771397037, 54.0588453495766, 51.1170782368227, 
    46.8800604490026, 39.3644742856029, 25.8834017175556, 7.9801308355807, 
    0.195382722914001), DLCE4 = c(31.8570261713385, 0.25009748180922, 
    0.209897168793501, 0.15484791604718, 0.0948240785715274, 
    0.0614002092775255, 0.0563958701235407, 0.0684838817269651, 
    0.0892028748168604, 0.0998945161796054, 0.112021762835994, 
    0.111909788729208, 0.124743055773804, 0.141123311513771, 
    0.159835345537721, 0.182104148939161, 0.210188267147751, 
    0.246003670536013, 0.294870716758041, 0.357366008233493, 
    0.462384124454952, 0.664148164486512, 0.979121056602319, 
    1.16907226723123), DLCE7 = c(4.77540419746678, 1.50376431485265, 
    1.47797646202944, 1.42669189641922, 1.32462884785723, 1.17093479302985, 
    1.02311690905089, 0.963588385751101, 1.02678990738926, 1.05976176090831, 
    1.10262684652118, 1.10257966713171, 1.15193367581346, 1.20070365929903, 
    1.25232708560117, 1.30650225412976, 1.36262302045392, 1.42949141916033, 
    1.50249598750593, 1.56104295210936, 1.59415875174203, 1.49897858381782, 
    1.05506561653598, 0.503148192870783), h = c(-75.4, -66, -56.6, 
    -47.2, -37.8, -28.35, -18.9, -9.45, 0, 6.25, 12.5, 12.5, 
    19.467, 26.433, 33.4, 40.96, 48.52, 56.08, 63.64, 70.84, 
    78.04, 85.24, 92.44, 99.64)), class = "data.frame", row.names = c(NA, 
-24L)))

Looking at the column names we have :

coln = lapply(samp, names)
coln
$`YUN_p07-i01_ClusterC2_ILA1`
 [1] "DLC12s"  "DLC17p"  "DLC17q"  "DLC21gs" "DLC24as" "DLC24bs" "DLC31s"  "DLC41es" "DLC41is" "DLC41ms" "DLC64h"  "DLC64l"  "DLC72"  
[14] "h"      

$`SAM_p01-i01_B45-03`
 [1] "DLC12"  "DLC24a" "DLC24b" "DLC31"  "DLC41e" "DLC41i" "DLC41m" "DLC64h" "DLC64l" "DLC72"  "h"     

$`NnG_p24-i02_c02_6P_M36`
 [1] "DLC12"  "DLC24a" "DLC24b" "DLC31"  "DLC41e" "DLC41i" "DLC41m" "DLC64h" "DLC64l" "DLC72"  "DLCE4"  "DLCE7"  "h"     

h is height and is presented in all dataframes. I'm looking for a clean way to subset all the columns with the same name in one separate data frame.

ind = as.data.frame(table(unlist(coln)))
ind
      Var1 Freq
1    DLC12    2
2   DLC12s    1
3   DLC17p    1
4   DLC17q    1
5  DLC21gs    1
6   DLC24a    2
7  DLC24as    1
8   DLC24b    2
9  DLC24bs    1
10   DLC31    2
11  DLC31s    1
12  DLC41e    2
13 DLC41es    1
14  DLC41i    2
15 DLC41is    1
16  DLC41m    2
17 DLC41ms    1
18  DLC64h    3
19  DLC64l    3
20   DLC72    3
21   DLCE4    1
22   DLCE7    1
23       h    3

In this case I need 22 data frame. More specific, the column name DLC64h is present in all data frames so my subset should have 6 column for the DLC64h and its related h. The following just works if my selected column name in repreated in all data frames :

d1 = lapply(samp, function(x) x[, c(as.character(ind[18,1]),"h")])

But not in case it is not a common column name.

After selecting those subsets I have to plot them. One plot per unique column name.

7
  • 22, h is the variable that I need to plot all the other column names based on that. So I need to plot all the unique col.names based on their h values Commented May 11, 2020 at 18:03
  • exactely, if you look at something like d1 in my example, the same col.name from different data.frame in the list do not have the same length. I don't know something like NA should be good I assume, Since I have to plot them at the end Commented May 11, 2020 at 18:09
  • @akrun So, h is unique in each data.frame. in this example I have 3 dataframe in the list with 3 h which are different . in case we would like to plot a column which is repeated in each data.frame , let say DLC64h we have to plot 3 pair of data : DLC64h and h from the first data.frame, DLC64h and h from the second dataframe and DLC64h and h from the third data.frame. Commented May 11, 2020 at 18:22
  • @akrun, True ! if you look at ind object in my example, it says in the list , 2 data.frames has a column called DLC12 , 3 data.frame that has a column called DLC64h and so on. What is needed now is a plot each unique col.name. If it is repeated once , so it is just one plot of that col.name against the h. if it is more than one time repeated, then like DLC12, it should be 2 curve in the plot one in DLC12 and h from the second data.frame in the list and one DLC12 and h from the 3rd data.frame in the list Commented May 11, 2020 at 18:32
  • @akrun correct ! the plot name should be DLC31 and the legend are 2 and 3 Commented May 11, 2020 at 18:43

1 Answer 1

1

We get the unique column names from all the list elements ('un1'), loop over the names, extract the column names that are the same from each of the 'samp' in a nested lapply, use cbind.fill from rowr to cbind the list elements (while filling the unequal rows with NA for those datasets that have less number of rows) to create 'lst1'. Another list is created to get the index the list element where the column names comes from ('lst2'). Use these two lists in Map to extract the corresponding 'h' column based on the index from 'lst2', and cbind with each of the datasets of 'lst1'

library(rowr)
un1 <- setdiff(unique(unlist(lapply(samp, names))), "h")     
lst1 <-  lapply(un1, function(nm) do.call(cbind.fill,
   c(Filter(length, lapply(samp, function(x)
        x[colnames(x) == nm])), fill = NA)))
lst2 <- lapply(un1, function(nm) which(do.call(c,
     lapply(samp, function(x)  any(names(x) == nm)))))
out <- Map(function(dat1, ind) {
      tmp <- do.call(cbind.fill, c(lapply(samp[ind], `[[`, 'h'), fill = NA))
      names(tmp) <- paste0("h", seq_along(tmp))
      cbind(dat1, tmp)},
       lst1, lst2)

length(out)
#[1] 22

-checking the output

lapply(out, head, 2)
#[[1]]
#    DLC12s      h1
#1 86.19998 -52.500
#2 83.16610 -43.375

#[[2]]
#     DLC17p      h1
#1 0.5184452 -52.500
#2 1.5012423 -43.375

#[[3]]
#     DLC17q      h1
#1 0.2929875 -52.500
#2 0.3105346 -43.375

#[[4]]
#     DLC21gs      h1
#1 12.7175189 -52.500
#2  0.1544069 -43.375

#[[5]]
#    DLC24as      h1
#1 0.2228264 -52.500
#2 0.2411541 -43.375

#[[6]]
#     DLC24bs      h1
#1 0.02773543 -52.500
#2 0.04170485 -43.375

#[[7]]
#       DLC31s      h1
#1 0.001799534 -52.500
#2 0.451788609 -43.375

#[[8]]
#       DLC41es      h1
#1 0.0003281455 -52.500
#2 0.0094817520 -43.375

#[[9]]
#      DLC41is      h1
#1 0.001144196 -52.500
#2 0.369375492 -43.375

#[[10]]
#      DLC41ms      h1
#1 0.003163386 -52.500
#2 0.121520955 -43.375

#[[11]]
#       DLC64h     DLC64h    DLC64h      h1    h2    h3
#1 0.003437833 0.01828710 0.0682039 -52.500 -69.3 -75.4
#2 1.063494100 0.08393471 0.3838715 -43.375 -65.0 -66.0

#[[12]]
#        DLC64l     DLC64l       DLC64l      h1    h2    h3
#1 2.456927e-16 0.07751714 0.0491324765 -52.500 -69.3 -75.4
#2 1.902683e+00 0.13670254 0.0006464645 -43.375 -65.0 -66.0

#[[13]]
#        DLC72    DLC72     DLC72      h1    h2    h3
#1  0.01063255 12.82851  8.336495 -52.500 -69.3 -75.4
#2 10.66651137 27.71747 36.174530 -43.375 -65.0 -66.0

#[[14]]
#     DLC12    DLC12    h1    h2
#1 86.53149 54.44353 -69.3 -75.4
#2 70.64820 60.40582 -65.0 -66.0

#[[15]]
#     DLC24a    DLC24a    h1    h2
#1 0.2187664 0.1598862 -69.3 -75.4
#2 0.1533400 0.1716777 -65.0 -66.0

#[[16]]
#      DLC24b     DLC24b    h1    h2
#1 0.04532141 0.01841368 -69.3 -75.4
#2 0.04852150 0.02924072 -65.0 -66.0

#[[17]]
#      DLC31     DLC31    h1    h2
#1 0.1142758 0.1051915 -69.3 -75.4
#2 0.4196964 0.3760683 -65.0 -66.0

#[[18]]
#       DLC41e      DLC41e    h1    h2
#1 0.001120229 0.001992596 -69.3 -75.4
#2 0.005298573 0.009939579 -65.0 -66.0

#[[19]]
#     DLC41i    DLC41i    h1    h2
#1 0.1384648 0.0763053 -69.3 -75.4
#2 0.6957711 0.4806988 -65.0 -66.0

#[[20]]
#      DLC41m    DLC41m    h1    h2
#1 0.02624807 0.1084238 -69.3 -75.4
#2 0.09105723 0.2136423 -65.0 -66.0

#[[21]]
#       DLCE4    h1
#1 31.8570262 -75.4
#2  0.2500975 -66.0

#[[22]]
#     DLCE7    h1
#1 4.775404 -75.4
#2 1.503764 -66.0

If we don't have rowr, then an option is to create rows for the list elements that have less number of rows with NA

un1 <- setdiff(unique(unlist(lapply(samp, names))), "h")   
lst1 <- lapply(un1, function(nm)  {
  tmplst <- Filter(length, lapply(samp, function(x) 
      x[colnames(x) == nm]))
  mx <- max(sapply(tmplst, nrow))
  do.call(cbind, lapply(tmplst, function(x) {
        if(mx > nrow(x))  x[nrow(x):mx, ] <- NA
       x}))})

 lst2 <- lapply(un1, function(nm) which(do.call(c,
       lapply(samp, function(x)  any(names(x) == nm)))))


out <- Map(function(dat1, ind) {
      tmplst <-  lapply(samp[ind], `[[`, 'h')
      mx <- max(lengths(tmplst))
      tmplst1 <- do.call(cbind, lapply(tmplst, `length<-`, mx))
      colnames(tmplst1) <- paste0('h', seq_len(ncol(tmplst1)))
      cbind(dat1, tmplst1)
      }, lst1, lst2)



sapply(out, dim)
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] #[,15] [,16] [,17] [,18] [,19] [,20]
#[1,]   22   22   22   22   22   22   22   22   22    22    38    38    38    38    #38    38    38    38    38    38
#[2,]    2    2    2    2    2    2    2    2    2     2     6     6     6     4     #4     4     4     4     4     4
#     [,21] [,22]
#[1,]    24    24
#[2,]     2     2

Update

With the named list, we can change the

 colnames(tmplst1) <- paste0('h', seq_len(ncol(tmplst1)))

to

colnames(tmplst1) <- paste0('h', colnames(tmplst1))

ie.

out <- Map(function(dat1, ind) {
   tmplst <-  lapply(samp[ind], `[[`, 'h')
   mx <- max(lengths(tmplst))
   tmplst1 <- do.call(cbind, lapply(tmplst, `length<-`, mx))
   colnames(tmplst1) <- paste0('h', colnames(tmplst1))
   cbind(dat1, tmplst1)
    }, lst1, lst2)
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.