Home > Software design >  How to put together and provide a similar-case-based order to three datasets using dplyr library?
How to put together and provide a similar-case-based order to three datasets using dplyr library?

Time:10-08

I have three datasets I'm working:

DATASET 1

structure(list(ID = c("01", "04", "06", "07", "08", "09", "10", 
"11", "12", "13", "15", "16", "17", "18", "19", "21", "22", "23", 
"25", "27", "28", "30", "44", "46", "49"), GR = c("RP", "RP", 
"RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
"RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
"RP"), SES = c("V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
"V", "V"), COND = c("NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", 
"NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", 
"NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", 
"NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", 
"NEG-CTR", "NEG-CTR", "NEG-CTR"), `P3(400-450).FCz` = c(-11.6312151716924, 
-0.314155675382471, -0.213621915029167, -2.83109425298642, 4.23849942428043, 
9.57481668808606, -6.13036076093477, 0.528517585832501, -7.73588468896919, 
1.27327945355082, -1.83310726842883, 4.37524298634374, 0.0405517582137798, 
-5.73445509910268, 2.95352732001065, -3.46703346467546, -7.27028665849262, 
2.65765002364624, 1.85698518142405, -7.04782633579147, 4.56661945182626, 
-6.10523962206958, -2.61258020387919, -4.63162466544638, -4.22719611676328
), `P3(400-450).Cz` = c(-5.16524399006139, 2.23100741241039, 
-1.30019374375434, 2.19344184533265, 2.44958531767688, 13.7118233181713, 
-4.13240668697805, 5.42592103255673, -2.40677805475299, 3.48359241788107, 
1.48755216347718, 3.89476490634811, 1.9413417416824, -2.76146409940467, 
1.74783962328435, -3.31557794753334, -8.1566939611461, 0.906302081219897, 
4.0336657306528, -4.6805488231028, 4.96594373869991, -5.61465293602653, 
-0.0325220589039272, -1.6547144697087, -1.42281778522059), `P3(400-450).Pz` = c(11.8802266972569, 
15.0981004360619, 3.13771453335467, 13.6778924406572, 7.47946451329025, 
23.4166601996042, 6.267521071803, 12.9998037913548, 15.7899796085713, 
9.93797956768228, 5.16656503460515, 10.0135486953849, 8.64382513728869, 
4.35678644331281, 4.93825547529124, 6.54487921689425, -0.856423579793706, 
5.6617683754256, 6.59026080217083, 1.63892755704177, 10.7031907391191, 
6.04891324234645, 14.4393884282958, 7.1182095475238, 9.15473202689768
), `LPPearly(500-700).FCz` = c(-11.7785042972793, -4.01515836011381, 
4.46729570509601, -0.883620011106743, 1.14544537612393, 13.2986259796748, 
0.0229069420708053, 1.89972383690448, -8.08292381883298, 1.75469999857951, 
2.55445787016256, 2.61316333850434, 0.148635887703097, -7.82526758429289, 
2.36752476749952, -4.5365387390683, 0.498995124872827, 0.901948386281446, 
0.155897732673534, -1.16182571974245, 4.3385479368043, -6.26956613362656, 
-0.888293709383838, -2.958120275175, -4.41290818553442), `LPPearly(500-700).Cz` = c(-5.96429031525769, 
-1.43557366487622, 3.52155765271648, 5.49897156207812, -0.434363848460157, 
17.2093269365993, 4.95253363860044, 6.1294488368639, -1.24197194087055, 
4.22150266269492, 4.19096721581768, 2.0809543999959, 3.05879540677983, 
-2.77155065610474, 2.24515223348079, -2.00968595029144, 0.588399071755827, 
-0.338674980283045, 2.55036185373462, 1.60091251589958, 4.8452288234686, 
-4.5951256708181, 3.15655538248828, -0.0391030568720636, -1.7348388034111
), `LPPearly(500-700).Pz` = c(8.23981597718437, 5.06332653216481, 
7.56089165217984, 13.0320953572069, 3.25309970442897, 25.567439566524, 
11.9200281736866, 10.2974302220899, 12.4817579327688, 9.63063375751153, 
3.35694102903017, 6.9103658689166, 8.5369185279747, 4.39617687043259, 
5.34939694712468, 7.38032829587839, 6.24066579989613, 3.55732294589389, 
2.32002496709926, 6.39254974438057, 6.23573445580928, 3.10850022259445, 
13.5899217198075, 5.04464304009428, 5.62233873107127), `LPP1(500-1000).FCz` = c(-5.67295796971287, 
0.159183652691071, 3.17401220482225, 2.41479226362682, 1.17012593697048, 
12.7528246377871, -0.53593239213453, 3.33072474503479, -6.16029227437628, 
3.03797140797724, 4.42881386089238, 2.81796777502117, 2.07031118447562, 
-7.67207306345715, 2.76956021356389, -0.318029598802272, 4.23210597331405, 
1.85997966736632, 0.972224468902529, 1.93604180609212, 2.119167943437, 
-5.39742090103356, 0.0347244526123614, -4.33933038854005, -2.08242541523686
), `LPP1(500-1000).Cz` = c(-0.774461731301161, 1.51403741206392, 
2.74763442988403, 6.55857216695288, -0.362612239707932, 16.5065564383967, 
3.56318544046926, 6.49648576604693, 0.407191700432703, 5.1987079378275, 
5.56965921078155, 2.27122847213408, 4.34660284150802, -3.75158080397933, 
2.71639671748793, 1.81478467865213, 3.72063461781626, 0.149229976075454, 
2.47692422786086, 3.71871325602406, 1.85724439689509, -5.10749483497166, 
1.97916368442935, -1.76271721053775, -0.080319620782262), `LPP1(500-1000).Pz` = c(9.99385579756163, 
3.7899021820967, 6.34418311104072, 11.8912196156261, 1.04545904874305, 
22.137894648313, 8.53744587416335, 7.92145847204293, 13.2326207795965, 
8.65720626559161, 3.6553384403983, 5.12280569940421, 7.74511728369855, 
2.00057785517691, 5.46564282736687, 7.33605094723817, 6.79108056396903, 
3.23055080248757, 1.16267428814891, 7.26747194148891, 3.51813473706825, 
2.44180972822096, 8.83165959849771, 2.60112153747699, 5.16871756657453
), `LPP2(1000-1500).FCz` = c(-0.198736254963744, 3.11042068112836, 
0.70169995101623, 6.69246804853767, 1.37744267413711, 4.70492544977759, 
-3.27424811388479, -0.548079192716476, -2.03662250850588, 1.74578985593211, 
4.7071611424039, 0.112840808931107, -1.0319619837514, -3.37172556803734, 
5.00204428574547, 2.89875624368901, 5.10788499683596, 4.13801289432792, 
0.951033896893375, 3.74570635774348, -0.953752628300353, -2.5380125964518, 
-0.0562425891448048, -6.30600497325119, 3.42195795553968), `LPP2(1000-1500).Cz` = c(2.96437294922766, 
3.44844607014521, -0.119440173019054, 7.32640154038155, 0.716894061961362, 
6.44896958389742, -0.881527211868382, 0.914180881345654, 2.10232992003589, 
2.92045510618672, 3.8646810953732, -2.43380575479143, 1.32843479186238, 
-2.57872703851591, 6.29976121968667, 2.80516686935069, 3.87077739797244, 
-0.837752514082802, 1.42192692667874, 3.70559987919106, -2.35132492869438, 
-4.61839904975041, 0.707894602445896, -4.65014144483362, 3.99189974753945
), `LPP2(1000-1500).Pz` = c(6.28027312932027, 1.08242973465635, 
1.22060069796986, 8.73650967214853, -1.33620384654018, 8.11715753014206, 
2.88799699943079, -1.35508717090199, 9.53009273459153, 3.75491412513142, 
1.3038251300445, -0.87603278792859, 2.01133614153975, -2.14054013397946, 
6.970012019096, 3.15582202707403, 4.70486363207892, 0.519683704125285, 
-1.38695689767217, 6.00477421965019, -0.897086547028692, -0.204119654251134, 
3.75503694959562, -2.7719104342547, 4.4514507366784)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -25L))

DATASET 2

structure(list(ID = c("01", "04", "06", "07", "08", "09", "10", 
"11", "12", "13", "15", "16", "17", "18", "19", "21", "22", "23", 
"25", "27", "28", "30", "44", "46", "49"), GR = c("RP", "RP", 
"RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
"RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
"RP"), SES = c("V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
"V", "V"), COND = c("NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", 
"NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", 
"NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", 
"NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", "NEU-NOC", 
"NEU-NOC", "NEU-NOC", "NEU-NOC"), `P3(400-450).FCz` = c(-3.99591470944713, 
5.03749946898385, -1.97168681693488, -6.692991645215, 3.5510699900835, 
1.7067607715475, -2.50672211111696, 1.88777321897925, -6.97442700196932, 
0.524299677616254, -0.261373488428192, 3.19617639836154, -0.381435057304614, 
-2.45744234877604, 1.91803490989119, -5.35374408162217, -5.39955520296854, 
6.24433066412776, -0.068523080368053, -6.62134671432544, 2.02997952225347, 
-3.97851995684846, -3.9210032516844, -6.71005969834916, -5.69725200870146
), `P3(400-450).Cz` = c(0.621502123415388, 7.75899775608441, 
-1.92340529575071, -5.92378937757888, 1.65779442628225, 5.31481098188172, 
0.142083484505352, 4.52018125654081, -1.55736964635117, 2.00051785325202, 
1.30346907198835, 4.36353100770575, -0.015852062711641, 1.01799377568815, 
2.16398550183836, -6.06703163736936, -4.35545141573936, 3.80236232314171, 
0.835589319243251, -5.50475999427345, 3.00187233307059, -5.20450410570445, 
-2.01375702882255, -4.48258340054462, -4.13736508533355), `P3(400-450).Pz` = c(12.955441582096, 
16.671036999147, 2.15365554736525, 6.86762877785576, 8.45962311067909, 
9.97183712753913, 10.6320358418368, 11.8852179570666, 14.3076172484818, 
5.04681800218272, 2.29899409536951, 7.81908431468528, 3.47474629234488, 
6.56231512354717, 6.81966900599588, 1.10301223694429, -0.931653372049331, 
5.1554825066748, 1.80359068950898, 0.769316188513939, 8.01071628743378, 
3.46633322373091, 7.74933708914689, 0.627435381320771, 7.14786907480758
), `LPPearly(500-700).FCz` = c(-7.58190508537766, -2.02861964460179, 
2.22923889930115, -2.0629672230873, 0.0422658298956365, 3.93467692474742, 
0.237726051904304, 0.318100791495115, -15.7998485301436, -1.8397955509895, 
0.62462329496673, 1.76690658846479, -3.17305964093897, -4.39207076049089, 
-0.29794568443312, -2.70852853745588, 0.269498974991661, 1.63525170542944, 
-0.856727109535223, -2.21433585407388, 0.127710423625772, -7.25886366924741, 
-2.42141595261389, -5.32347488769128, -4.23649270310915), `LPPearly(500-700).Cz` = c(-2.81732229625975, 
0.160393685024631, 2.70693992810407, -1.37301251388987, -1.27152670283348, 
8.11964589961276, 4.15235381401148, 0.783414018677801, -9.79031812534203, 
0.208005397351335, 1.48137456347872, 2.84860039832237, -0.269002712326028, 
0.17928456999128, -0.0920286086411814, -1.27622011197768, 1.96978732491278, 
-0.0154791822607025, -0.599156247027551, -0.192490723623988, 
1.02554478706585, -7.42770276334892, -0.345116641695513, -2.71685194532693, 
-2.4844653851482), `LPPearly(500-700).Pz` = c(9.42367409925817, 
9.07903916629231, 4.91476855238182, 5.86927622259489, 2.71096740085175, 
13.0990192799703, 9.67397418905514, 5.9401530589224, 1.00612108990875, 
3.08031473770521, -0.133592200169464, 6.75669517393108, 4.47409706618326, 
6.06169418872804, 4.85729686493463, 4.95764559864061, 4.07498375647916, 
0.956782967443242, -0.832211906889126, 2.97245026797807, 2.98791225155534, 
-0.00459651443883508, 8.10285456644801, 1.0510618938653, 5.87476640145049
), `LPP1(500-1000).FCz` = c(-2.96652960658775, 1.97377908783621, 
2.27932356239417, 1.37236796086306, -0.0352287346576431, 5.29087810500407, 
-0.350235251052037, 0.368353479250859, -11.6929994675865, -0.105609513369084, 
0.790366882022175, 0.481312527645089, -1.6987207178219, -3.70465330706651, 
-0.117663849819739, 0.495791777472788, 4.2493002120623, 4.49876374949007, 
-1.88216552531471, 0.396199418616196, -0.797780510446411, -7.90158650835565, 
0.71379455593, -4.99161397741678, -2.90677423807339), `LPP1(500-1000).Cz` = c(1.14010250644923, 
3.76051565494304, 3.29833843753872, 0.6053954850232, -1.9659602005953, 
9.47863905985504, 2.81836003896073, 0.0583773785365037, -6.69531720196194, 
1.95912937005718, 1.73555945138244, 1.08565421371063, 1.13778488337031, 
1.11347399164258, -0.0125553251938872, 1.84877511449264, 6.11884162455143, 
2.26488777144961, -2.36683956150553, 2.16160561308748, -0.0613289083097637, 
-9.5077807770796, 2.05005047140863, -2.94229298615359, -1.30897538037428
), `LPP1(500-1000).Pz` = c(10.6989716871958, 8.52123662617732, 
4.94124365003338, 5.33467168055784, 0.456014907516276, 12.3737995164614, 
6.44029646492883, 3.67962142328031, 1.19234165952119, 3.56217081729065, 
-0.955862637061474, 4.20070031588935, 3.53005530275895, 6.27767029539405, 
4.21138025611764, 4.92138251507151, 4.88215501512899, 2.24719486660004, 
-2.63749003510716, 3.85188130526715, 1.35864978279665, -0.909253908918845, 
6.3336610852979, -0.385577376783475, 5.14162645294768), `LPP2(1000-1500).FCz` = c(0.895992279831378, 
3.83846437952292, -1.33916628569866, 5.6387334295083, 1.98775658048965, 
1.04588866129981, -0.737029533303028, 2.83406431170166, -4.10197843548265, 
1.0088582830766, 3.41865997847206, 1.68471621974079, -0.36155713377909, 
0.276424687829489, 1.76823718363874, 1.68068636794087, 8.57093653637002, 
7.14602235806343, -0.245809508894366, 0.585956207256955, -1.52627773487111, 
-3.39003001417816, 0.14533441948593, -5.38510065662888, 1.56160375018352
), `LPP2(1000-1500).Cz` = c(2.94619035115619, 4.7045767546583, 
-0.405630185540259, 1.59973104216962, -0.0113614476558772, 3.79329103591037, 
0.547336389166803, 0.544379787211248, -2.01625839730052, 2.47669253216776, 
3.99379121188326, 1.8064338549701, 2.22142205600503, 3.56904822058867, 
2.05354063483883, 2.24420650476028, 8.04385138833399, 3.94854087847063, 
-1.26656454994214, 1.54527082821889, -1.87474139426298, -4.54674069848035, 
1.06029338143336, -4.32939541342964, 3.38297407425817), `LPP2(1000-1500).Pz` = c(7.68162285335806, 
5.36085942954182, 0.476667611076957, 4.57722295263921, -0.547579465083117, 
2.03237948843614, 0.224312643096751, -0.524427185444798, 0.354581513905486, 
1.13298849399454, -0.982935199970812, 2.67697914555442, 1.82011380148253, 
3.4064978775436, 4.05060374186422, 1.54086695776033, 4.86622757700525, 
2.66173787497657, -2.24262472628189, 1.13798685231667, -0.589472259038692, 
1.9188582401756, 2.2112162248386, -2.66041249378004, 8.18987741895779
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-25L))

DATASET 3

structure(list(ID = c("01", "04", "06", "07", "08", "09", "10", 
"11", "12", "13", "15", "16", "17", "18", "19", "21", "22", "23", 
"25", "27", "28", "30", "44", "46", "49"), GR = c("RP", "RP", 
"RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
"RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
"RP"), SES = c("V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
"V", "V"), COND = c("NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", 
"NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", 
"NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", 
"NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", "NEG-NOC", 
"NEG-NOC", "NEG-NOC", "NEG-NOC"), `P3(400-450).FCz` = c(-11.1438413285935, 
0.238885648959708, -2.96032491743069, 1.09291198163802, 2.9898889629932, 
5.4167795618285, -2.82955734597919, 8.16418133488309, -9.83058052401056, 
2.11962397764132, 0.658810483381172, 0.625555654900511, -3.29357103412113, 
-6.1129152355645, 0.527721249096473, -2.40438419043702, -7.1532211375959, 
0.372495441513391, -0.564454675803529, -4.52263283590558, 3.05859761335498, 
-0.521871236969702, -4.13974828699279, -4.36762718685405, -0.229916506217565
), `P3(400-450).Cz` = c(-5.53112490175437, 3.96990710862955, 
-3.59899040898949, 5.87900720863083, 3.10043497883256, 6.86178446511352, 
0.162182285588285, 14.5496375672716, -5.3832670295207, 4.23167123533126, 
2.37269462739372, 1.87516303240986, -2.22114447555529, -3.51627712447581, 
1.1303870721987, -1.83920975041768, -9.23030396302541, 0.45401759063429, 
2.0185967445137, -1.20746167339041, 4.1349308440931, 0.544596077279702, 
-2.28038421035601, -0.619979893871085, 2.62315679073783), `P3(400-450).Pz` = c(12.1053426662461, 
15.4046229884164, -0.0892565159000666, 14.3862738306396, 8.93405130318593, 
15.1868092142896, 10.142198458411, 20.7052065690674, 7.50729833890206, 
10.7693238464384, 7.87875085817396, 5.48278706243332, 3.35777109534179, 
3.47085321062162, 7.33985613752315, 7.25872117706077, -0.887835692028378, 
2.29939831067085, 3.0741733363644, 3.857933716935, 9.53278894637555, 
11.1964453850602, 11.2339563353478, 7.39260082121406, 13.6559037433263
), `LPPearly(500-700).FCz` = c(-9.14927207125904, -6.60165385653499, 
2.54036572774646, -2.63569087592267, 2.08056674659401, 5.06669915366333, 
4.31923128857779, 3.2371880079134, -5.73174008540523, 0.677370118816266, 
-0.380810453692585, 2.68202480583985, -0.958853757041888, -6.58557573679886, 
0.594715760553033, 0.196832250811775, 0.165171574219401, -2.45955661653299, 
1.8491735212703, 1.07658425742917, 4.40588599635354, -1.17658595005389, 
-2.14177059335841, -5.1274001953303, -1.21404719262173), `LPPearly(500-700).Cz` = c(-5.10918437158799, 
-3.14872157912645, 2.10437989449921, 5.81171180245335, 2.87987510596148, 
7.79412746755931, 9.50695673265293, 8.01447499455337, -0.487178595894761, 
4.20139847550095, 0.815283302847055, 4.35199943309111, 2.11976068962167, 
-2.59002218694999, 1.88805943988563, 2.59427260100332, -1.43982473126936, 
-1.86484698930706, 4.42520405730058, 4.7367320574401, 5.71745745981867, 
1.1704842909792, -0.639830772856786, -2.61585906518491, 1.00287124847525
), `LPPearly(500-700).Pz` = c(9.51261484648731, 5.02619159395405, 
5.49719893790597, 10.8414516494484, 4.6847880297099, 16.3241813617706, 
14.6901305277101, 12.0768070828642, 12.419526465857, 10.5631237176538, 
4.28046277054405, 7.64737651416791, 7.08645126073423, 3.27924738047746, 
5.58288092654703, 11.7259526759912, 3.49843659402445, 1.33918111568512, 
3.15289777246607, 7.0533787627062, 7.6052386193207, 8.12060882554471, 
9.93070913311253, 2.02262615478956, 10.1193593084848), `LPP1(500-1000).FCz` = c(-4.3918290080777, 
-1.78361184935376, 1.60211665164145, -0.16792378673807, 1.40362599894122, 
8.65746780516039, 5.14963479715099, 5.69883686071382, -4.63273873600962, 
1.9835624834285, 2.52938643377794, 4.4468631569674, 1.88437037400787, 
-7.47167401086238, 0.170115859759874, 2.5952186989928, 7.04880390672238, 
-1.68653874227364, 0.283281427206851, 4.36911508867038, 2.78021097216721, 
-3.25249602443759, -0.812966906667064, -3.97555431948626, -0.770668513084632
), `LPP1(500-1000).Cz` = c(-0.650009462761383, 0.25571835554024, 
1.09116707485702, 7.06029312947389, 2.72729847885135, 9.68783611391578, 
9.23934223584773, 8.95767573723818, -0.355920498472261, 4.46546567977094, 
3.02810334275795, 5.67690071529437, 4.36701185856921, -3.70816720802515, 
2.04095164097647, 5.51311465032187, 5.69841082671879, -1.21110028746356, 
3.0362097791307, 6.88800630791438, 3.89090518078692, -1.91878923633278, 
0.0574790574701361, -1.9052154727859, 1.67927888371665), `LPP1(500-1000).Pz` = c(11.1212652173052, 
4.59413830322224, 4.10771312429751, 10.1332638103233, 2.86650202467156, 
16.2554496910698, 12.0085617194691, 11.0671124200944, 10.6863371331174, 
9.10677414828206, 4.92332669673837, 7.96701055540939, 6.81338213343676, 
0.307336075640122, 5.31077105909017, 10.9042628775492, 7.2234966296861, 
1.67062187720051, 1.66931713099581, 7.40312084435906, 4.96827359204273, 
3.52588208615053, 7.35889570568267, 1.24372245314509, 8.83562199570849
), `LPP2(1000-1500).FCz` = c(-3.16101041766438, 2.27800090558473, 
1.75290674648806, 4.76363910849669, 0.973396686787801, 12.1173859570715, 
3.17906097432267, 4.61956745266038, -1.04583230182887, 0.337182270006865, 
2.91357065338633, 3.65320446800974, -1.20400880603756, -4.73982223428082, 
2.59240054429885, 3.26725194423518, 10.9438273543919, -0.0919103435752493, 
-3.50828531999296, 3.35958383952017, 0.198016547101522, -3.87040246779716, 
-1.93150124231358, -4.05937234925567, 4.06645814493399), `LPP2(1000-1500).Cz` = c(-2.12913230708907, 
3.02403433835637, 0.668265457194833, 6.75063445570149, 1.15587576192688, 
10.5178519351064, 4.99936090838488, 4.57223746317305, 0.364087929362296, 
0.589250314392448, 1.39548469719622, 3.55543459632094, 0.998168707936746, 
-2.47010661770972, 4.17893812674545, 5.94481009988668, 7.51898916194332, 
-0.472475810660207, 0.41259599596416, 4.95053154685134, 1.27825568281823, 
-2.75088839785752, -2.39741431959044, -3.37506398767823, 5.66199164861177
), `LPP2(1000-1500).Pz` = c(5.24535230966772, 2.99896314000211, 
0.43007709818575, 7.20593389743811, -1.28885901608471, 13.3060153720352, 
5.57921718107867, 5.24004334238142, 5.96824937367958, 1.62293479252637, 
2.21921258546698, 4.07313151833664, 0.713386363228727, -2.91688430317331, 
4.8346929061964, 6.30033458860474, 7.02302307690502, 3.22734666260273, 
-1.01240056675658, 4.20438929142231, 2.0228708306979, 1.34241259142242, 
2.11369999794381, -3.28212379920159, 8.01351241284005)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -25L))

I would like merge them by variables:

ID 
GR
SES

and keep into the final merged dataset the same order they have of the original one, with the same sequence of all the variables as they are sort in, namely:

ID 
GR 
SES 
COND (all three conditions columsn coming from the three different datsets one after the other); 
P3 (400-450).Fcz = same order as for conditions; 
P3 (400-450).Cz = same as above; 
P3 (400 -450).Pz = same the three one above; 

and so on for all the following variables. My question is how I could do this all in one command by using dplyr %>% operator. However, if you don't know how to figure this out, every method you could suggest will be appreciated.

CodePudding user response:

Well, I don't get it working in one pipe (maybe someone else does, probably with some weird purrr magic), but this one does the trick as well. It's basically first getting the var names, then re-shuffling them into your desired order and then relocate them in the joined df.

df_join <- df1 %>%
  left_join(df2, by = c("ID", "GR", "SES"), suffix = c("", ".y")) %>%
  left_join(df3, by = c("ID", "GR", "SES"), suffix = c("", ".z"))

names_df1 <- (df_join %>%
  select(-ends_with(".y"),
         -ends_with(".z")) %>%
  names())[-c(1:3)]

names_df2 <- df_join %>%
  select(ends_with(".y")) %>%
  names()

names_df3 <- df_join %>%
  select(ends_with(".z")) %>%
  names()

names_all <- data.frame(names_df1, names_df2, names_df3) %>%
  rowwise() %>%
  mutate(names_all = list(c_across(everything()))) %>%
  ungroup() %>%
  pull(names_all) %>%
  unlist()

df_join <- df_join %>%
  relocate(1:3, all_of(names_all))

str(df_join)

tibble [25 x 42] (S3: tbl_df/tbl/data.frame)
 $ ID                     : chr [1:25] "01" "04" "06" "07" ...
 $ GR                     : chr [1:25] "RP" "RP" "RP" "RP" ...
 $ SES                    : chr [1:25] "V" "V" "V" "V" ...
 $ COND                   : chr [1:25] "NEG-CTR" "NEG-CTR" "NEG-CTR" "NEG-CTR" ...
 $ COND.y                 : chr [1:25] "NEU-NOC" "NEU-NOC" "NEU-NOC" "NEU-NOC" ...
 $ COND.z                 : chr [1:25] "NEG-NOC" "NEG-NOC" "NEG-NOC" "NEG-NOC" ...
 $ P3(400-450).FCz        : num [1:25] -11.631 -0.314 -0.214 -2.831 4.238 ...
 $ P3(400-450).FCz.y      : num [1:25] -4 5.04 -1.97 -6.69 3.55 ...
 $ P3(400-450).FCz.z      : num [1:25] -11.144 0.239 -2.96 1.093 2.99 ...
 $ P3(400-450).Cz         : num [1:25] -5.17 2.23 -1.3 2.19 2.45 ...
 $ P3(400-450).Cz.y       : num [1:25] 0.622 7.759 -1.923 -5.924 1.658 ...
 $ P3(400-450).Cz.z       : num [1:25] -5.53 3.97 -3.6 5.88 3.1 ...
 $ P3(400-450).Pz         : num [1:25] 11.88 15.1 3.14 13.68 7.48 ...
 $ P3(400-450).Pz.y       : num [1:25] 12.96 16.67 2.15 6.87 8.46 ...
 $ P3(400-450).Pz.z       : num [1:25] 12.1053 15.4046 -0.0893 14.3863 8.9341 ...
 $ LPPearly(500-700).FCz  : num [1:25] -11.779 -4.015 4.467 -0.884 1.145 ...
 $ LPPearly(500-700).FCz.y: num [1:25] -7.5819 -2.0286 2.2292 -2.063 0.0423 ...
 $ LPPearly(500-700).FCz.z: num [1:25] -9.15 -6.6 2.54 -2.64 2.08 ...
 $ LPPearly(500-700).Cz   : num [1:25] -5.964 -1.436 3.522 5.499 -0.434 ...
 $ LPPearly(500-700).Cz.y : num [1:25] -2.82 0.16 2.71 -1.37 -1.27 ...
 $ LPPearly(500-700).Cz.z : num [1:25] -5.11 -3.15 2.1 5.81 2.88 ...
 $ LPPearly(500-700).Pz   : num [1:25] 8.24 5.06 7.56 13.03 3.25 ...
 $ LPPearly(500-700).Pz.y : num [1:25] 9.42 9.08 4.91 5.87 2.71 ...
 $ LPPearly(500-700).Pz.z : num [1:25] 9.51 5.03 5.5 10.84 4.68 ...
 $ LPP1(500-1000).FCz     : num [1:25] -5.673 0.159 3.174 2.415 1.17 ...
 $ LPP1(500-1000).FCz.y   : num [1:25] -2.9665 1.9738 2.2793 1.3724 -0.0352 ...
 $ LPP1(500-1000).FCz.z   : num [1:25] -4.392 -1.784 1.602 -0.168 1.404 ...
 $ LPP1(500-1000).Cz      : num [1:25] -0.774 1.514 2.748 6.559 -0.363 ...
 $ LPP1(500-1000).Cz.y    : num [1:25] 1.14 3.761 3.298 0.605 -1.966 ...
 $ LPP1(500-1000).Cz.z    : num [1:25] -0.65 0.256 1.091 7.06 2.727 ...
 $ LPP1(500-1000).Pz      : num [1:25] 9.99 3.79 6.34 11.89 1.05 ...
 $ LPP1(500-1000).Pz.y    : num [1:25] 10.699 8.521 4.941 5.335 0.456 ...
 $ LPP1(500-1000).Pz.z    : num [1:25] 11.12 4.59 4.11 10.13 2.87 ...
 $ LPP2(1000-1500).FCz    : num [1:25] -0.199 3.11 0.702 6.692 1.377 ...
 $ LPP2(1000-1500).FCz.y  : num [1:25] 0.896 3.838 -1.339 5.639 1.988 ...
 $ LPP2(1000-1500).FCz.z  : num [1:25] -3.161 2.278 1.753 4.764 0.973 ...
 $ LPP2(1000-1500).Cz     : num [1:25] 2.964 3.448 -0.119 7.326 0.717 ...
 $ LPP2(1000-1500).Cz.y   : num [1:25] 2.9462 4.7046 -0.4056 1.5997 -0.0114 ...
 $ LPP2(1000-1500).Cz.z   : num [1:25] -2.129 3.024 0.668 6.751 1.156 ...
 $ LPP2(1000-1500).Pz     : num [1:25] 6.28 1.08 1.22 8.74 -1.34 ...
 $ LPP2(1000-1500).Pz.y   : num [1:25] 7.682 5.361 0.477 4.577 -0.548 ...
 $ LPP2(1000-1500).Pz.z   : num [1:25] 5.25 3 0.43 7.21 -1.29 ...

Another solution could be (assuming that all your three data sets are in the same, correct order) to just create a loop that selects the first three columns, then column 4 from df1, column 4 from df2, column 4 from df3, then the 5th column for each data set, then the 6th and so on.

  • Related