如何使用dplyr库将三个数据集放在一起并提供基于相似案例的顺序?

oxiaedzo  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(85)

我在处理三个数据集:

数据集1

structure(list(var_1 = c(1, 4, 6, 7, 8, 9, 10, 11, 12, 13, 15, 
16, 17, 18, 19, 21, 22, 23, 25, 27, 28, 30, 44, 46, 49), var_2 = c("soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap"
), var_3 = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A"), var_4 = c(988.368784828308, 999.685844324618, 999.786378084971, 
997.168905747014, 1004.23849942428, 1009.57481668809, 993.869639239065, 
1000.52851758583, 992.264115311031, 1001.27327945355, 998.166892731571, 
1004.37524298634, 1000.04055175821, 994.265544900897, 1002.95352732001, 
996.532966535325, 992.729713341507, 1002.65765002365, 1001.85698518142, 
992.952173664209, 1004.56661945183, 993.89476037793, 997.387419796121, 
995.368375334554, 995.772803883237), var_5 = c(994.834756009939, 
1002.23100741241, 998.699806256246, 1002.19344184533, 1002.44958531768, 
1013.71182331817, 995.867593313022, 1005.42592103256, 997.593221945247, 
1003.48359241788, 1001.48755216348, 1003.89476490635, 1001.94134174168, 
997.238535900595, 1001.74783962328, 996.684422052467, 991.843306038854, 
1000.90630208122, 1004.03366573065, 995.319451176897, 1004.9659437387, 
994.385347063973, 999.967477941096, 998.345285530291, 998.577182214779
), var_6 = c(1011.88022669726, 1015.09810043606, 1003.13771453335, 
1013.67789244066, 1007.47946451329, 1023.4166601996, 1006.2675210718, 
1012.99980379135, 1015.78997960857, 1009.93797956768, 1005.16656503461, 
1010.01354869538, 1008.64382513729, 1004.35678644331, 1004.93825547529, 
1006.54487921689, 999.143576420206, 1005.66176837543, 1006.59026080217, 
1001.63892755704, 1010.70319073912, 1006.04891324235, 1014.4393884283, 
1007.11820954752, 1009.1547320269), var_7 = c(988.221495702721, 
995.984841639886, 1004.4672957051, 999.116379988893, 1001.14544537612, 
1013.29862597967, 1000.02290694207, 1001.8997238369, 991.917076181167, 
1001.75469999858, 1002.55445787016, 1002.6131633385, 1000.1486358877, 
992.174732415707, 1002.3675247675, 995.463461260932, 1000.49899512487, 
1000.90194838628, 1000.15589773267, 998.838174280258, 1004.3385479368, 
993.730433866373, 999.111706290616, 997.041879724825, 995.587091814466
), var_8 = c(994.035709684742, 998.564426335124, 1003.52155765272, 
1005.49897156208, 999.56563615154, 1017.2093269366, 1004.9525336386, 
1006.12944883686, 998.758028059129, 1004.22150266269, 1004.19096721582, 
1002.0809544, 1003.05879540678, 997.228449343895, 1002.24515223348, 
997.990314049709, 1000.58839907176, 999.661325019717, 1002.55036185373, 
1001.6009125159, 1004.84522882347, 995.404874329182, 1003.15655538249, 
999.960896943128, 998.265161196589), var_9 = c(1008.23981597718, 
1005.06332653216, 1007.56089165218, 1013.03209535721, 1003.25309970443, 
1025.56743956652, 1011.92002817369, 1010.29743022209, 1012.48175793277, 
1009.63063375751, 1003.35694102903, 1006.91036586892, 1008.53691852797, 
1004.39617687043, 1005.34939694712, 1007.38032829588, 1006.2406657999, 
1003.55732294589, 1002.3200249671, 1006.39254974438, 1006.23573445581, 
1003.10850022259, 1013.58992171981, 1005.04464304009, 1005.62233873107
), var_10 = c(994.327042030287, 1000.15918365269, 1003.17401220482, 
1002.41479226363, 1001.17012593697, 1012.75282463779, 999.464067607865, 
1003.33072474503, 993.839707725624, 1003.03797140798, 1004.42881386089, 
1002.81796777502, 1002.07031118448, 992.327926936543, 1002.76956021356, 
999.681970401198, 1004.23210597331, 1001.85997966737, 1000.9722244689, 
1001.93604180609, 1002.11916794344, 994.602579098966, 1000.03472445261, 
995.66066961146, 997.917574584763), var_11 = c(999.225538268699, 
1001.51403741206, 1002.74763442988, 1006.55857216695, 999.637387760292, 
1016.5065564384, 1003.56318544047, 1006.49648576605, 1000.40719170043, 
1005.19870793783, 1005.56965921078, 1002.27122847213, 1004.34660284151, 
996.248419196021, 1002.71639671749, 1001.81478467865, 1003.72063461782, 
1000.14922997608, 1002.47692422786, 1003.71871325602, 1001.8572443969, 
994.892505165028, 1001.97916368443, 998.237282789462, 999.919680379218
), var_12 = c(1009.99385579756, 1003.7899021821, 1006.34418311104, 
1011.89121961563, 1001.04545904874, 1022.13789464831, 1008.53744587416, 
1007.92145847204, 1013.2326207796, 1008.65720626559, 1003.6553384404, 
1005.1228056994, 1007.7451172837, 1002.00057785518, 1005.46564282737, 
1007.33605094724, 1006.79108056397, 1003.23055080249, 1001.16267428815, 
1007.26747194149, 1003.51813473707, 1002.44180972822, 1008.8316595985, 
1002.60112153748, 1005.16871756657), var_13 = c(999.801263745036, 
1003.11042068113, 1000.70169995102, 1006.69246804854, 1001.37744267414, 
1004.70492544978, 996.725751886115, 999.451920807283, 997.963377491494, 
1001.74578985593, 1004.7071611424, 1000.11284080893, 998.968038016249, 
996.628274431963, 1005.00204428575, 1002.89875624369, 1005.10788499684, 
1004.13801289433, 1000.95103389689, 1003.74570635774, 999.0462473717, 
997.461987403548, 999.943757410855, 993.693995026749, 1003.42195795554
), var_14 = c(1002.96437294923, 1003.44844607015, 999.880559826981, 
1007.32640154038, 1000.71689406196, 1006.4489695839, 999.118472788132, 
1000.91418088135, 1002.10232992004, 1002.92045510619, 1003.86468109537, 
997.566194245209, 1001.32843479186, 997.421272961484, 1006.29976121969, 
1002.80516686935, 1003.87077739797, 999.162247485917, 1001.42192692668, 
1003.70559987919, 997.648675071306, 995.38160095025, 1000.70789460245, 
995.349858555166, 1003.99189974754), var_15 = c(1006.28027312932, 
1001.08242973466, 1001.22060069797, 1008.73650967215, 998.66379615346, 
1008.11715753014, 1002.88799699943, 998.644912829098, 1009.53009273459, 
1003.75491412513, 1001.30382513004, 999.123967212071, 1002.01133614154, 
997.859459866021, 1006.9700120191, 1003.15582202707, 1004.70486363208, 
1000.51968370413, 998.613043102328, 1006.00477421965, 999.102913452971, 
999.795880345749, 1003.7550369496, 997.228089565745, 1004.45145073668
)), row.names = c(NA, -25L), class = c("tbl_df", "tbl", "data.frame"
))

数据集2

structure(list(var_1 = c(1, 4, 6, 7, 8, 9, 10, 11, 12, 13, 15, 
16, 17, 18, 19, 21, 22, 23, 25, 27, 28, 30, 44, 46, 49), var_2 = c("soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap"
), var_3 = c("B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B"), var_4 = c(996.004085290553, 1005.03749946898, 998.028313183065, 
993.307008354785, 1003.55106999008, 1001.70676077155, 997.493277888883, 
1001.88777321898, 993.025572998031, 1000.52429967762, 999.738626511572, 
1003.19617639836, 999.618564942695, 997.542557651224, 1001.91803490989, 
994.646255918378, 994.600444797032, 1006.24433066413, 999.931476919632, 
993.378653285675, 1002.02997952225, 996.021480043152, 996.078996748316, 
993.289940301651, 994.302747991299), var_5 = c(1000.62150212342, 
1007.75899775608, 998.076594704249, 994.076210622421, 1001.65779442628, 
1005.31481098188, 1000.14208348451, 1004.52018125654, 998.442630353649, 
1002.00051785325, 1001.30346907199, 1004.36353100771, 999.984147937288, 
1001.01799377569, 1002.16398550184, 993.932968362631, 995.644548584261, 
1003.80236232314, 1000.83558931924, 994.495240005727, 1003.00187233307, 
994.795495894296, 997.986242971177, 995.517416599455, 995.862634914667
), var_6 = c(1012.9554415821, 1016.67103699915, 1002.15365554737, 
1006.86762877786, 1008.45962311068, 1009.97183712754, 1010.63203584184, 
1011.88521795707, 1014.30761724848, 1005.04681800218, 1002.29899409537, 
1007.81908431469, 1003.47474629234, 1006.56231512355, 1006.819669006, 
1001.10301223694, 999.068346627951, 1005.15548250667, 1001.80359068951, 
1000.76931618851, 1008.01071628743, 1003.46633322373, 1007.74933708915, 
1000.62743538132, 1007.14786907481), var_7 = c(992.418094914622, 
997.971380355398, 1002.2292388993, 997.937032776913, 1000.0422658299, 
1003.93467692475, 1000.2377260519, 1000.3181007915, 984.200151469856, 
998.160204449011, 1000.62462329497, 1001.76690658846, 996.826940359061, 
995.607929239509, 999.702054315567, 997.291471462544, 1000.26949897499, 
1001.63525170543, 999.143272890465, 997.785664145926, 1000.12771042363, 
992.741136330753, 997.578584047386, 994.676525112309, 995.763507296891
), var_8 = c(997.18267770374, 1000.16039368502, 1002.7069399281, 
998.62698748611, 998.728473297166, 1008.11964589961, 1004.15235381401, 
1000.78341401868, 990.209681874658, 1000.20800539735, 1001.48137456348, 
1002.84860039832, 999.730997287674, 1000.17928456999, 999.907971391359, 
998.723779888022, 1001.96978732491, 999.984520817739, 999.400843752972, 
999.807509276376, 1001.02554478707, 992.572297236651, 999.654883358304, 
997.283148054673, 997.515534614852), var_9 = c(1009.42367409926, 
1009.07903916629, 1004.91476855238, 1005.86927622259, 1002.71096740085, 
1013.09901927997, 1009.67397418906, 1005.94015305892, 1001.00612108991, 
1003.08031473771, 999.866407799831, 1006.75669517393, 1004.47409706618, 
1006.06169418873, 1004.85729686493, 1004.95764559864, 1004.07498375648, 
1000.95678296744, 999.167788093111, 1002.97245026798, 1002.98791225156, 
999.995403485561, 1008.10285456645, 1001.05106189387, 1005.87476640145
), var_10 = c(997.033470393412, 1001.97377908784, 1002.27932356239, 
1001.37236796086, 999.964771265342, 1005.290878105, 999.649764748948, 
1000.36835347925, 988.307000532414, 999.894390486631, 1000.79036688202, 
1000.48131252765, 998.301279282178, 996.295346692933, 999.88233615018, 
1000.49579177747, 1004.24930021206, 1004.49876374949, 998.117834474685, 
1000.39619941862, 999.202219489554, 992.098413491644, 1000.71379455593, 
995.008386022583, 997.093225761927), var_11 = c(1001.14010250645, 
1003.76051565494, 1003.29833843754, 1000.60539548502, 998.034039799405, 
1009.47863905986, 1002.81836003896, 1000.05837737854, 993.304682798038, 
1001.95912937006, 1001.73555945138, 1001.08565421371, 1001.13778488337, 
1001.11347399164, 999.987444674806, 1001.84877511449, 1006.11884162455, 
1002.26488777145, 997.633160438494, 1002.16160561309, 999.93867109169, 
990.49221922292, 1002.05005047141, 997.057707013846, 998.691024619626
), var_12 = c(1010.6989716872, 1008.52123662618, 1004.94124365003, 
1005.33467168056, 1000.45601490752, 1012.37379951646, 1006.44029646493, 
1003.67962142328, 1001.19234165952, 1003.56217081729, 999.044137362938, 
1004.20070031589, 1003.53005530276, 1006.27767029539, 1004.21138025612, 
1004.92138251507, 1004.88215501513, 1002.2471948666, 997.362509964893, 
1003.85188130527, 1001.3586497828, 999.090746091081, 1006.3336610853, 
999.614422623217, 1005.14162645295), var_13 = c(1000.89599227983, 
1003.83846437952, 998.660833714301, 1005.63873342951, 1001.98775658049, 
1001.0458886613, 999.262970466697, 1002.8340643117, 995.898021564517, 
1001.00885828308, 1003.41865997847, 1001.68471621974, 999.638442866221, 
1000.27642468783, 1001.76823718364, 1001.68068636794, 1008.57093653637, 
1007.14602235806, 999.754190491106, 1000.58595620726, 998.473722265129, 
996.609969985822, 1000.14533441949, 994.614899343371, 1001.56160375018
), var_14 = c(1002.94619035116, 1004.70457675466, 999.59436981446, 
1001.59973104217, 999.988638552344, 1003.79329103591, 1000.54733638917, 
1000.54437978721, 997.9837416027, 1002.47669253217, 1003.99379121188, 
1001.80643385497, 1002.22142205601, 1003.56904822059, 1002.05354063484, 
1002.24420650476, 1008.04385138833, 1003.94854087847, 998.733435450058, 
1001.54527082822, 998.125258605737, 995.45325930152, 1001.06029338143, 
995.67060458657, 1003.38297407426), var_15 = c(1007.68162285336, 
1005.36085942954, 1000.47666761108, 1004.57722295264, 999.452420534917, 
1002.03237948844, 1000.2243126431, 999.475572814555, 1000.35458151391, 
1001.13298849399, 999.017064800029, 1002.67697914555, 1001.82011380148, 
1003.40649787754, 1004.05060374186, 1001.54086695776, 1004.86622757701, 
1002.66173787498, 997.757375273718, 1001.13798685232, 999.410527740961, 
1001.91885824018, 1002.21121622484, 997.33958750622, 1008.18987741896
)), row.names = c(NA, -25L), class = c("tbl_df", "tbl", "data.frame"
))

数据集3

structure(list(var_1 = c(1, 4, 6, 7, 8, 9, 10, 11, 12, 13, 15, 
16, 17, 18, 19, 21, 22, 23, 25, 27, 28, 30, 44, 46, 49), var_2 = c("soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap", 
"soap", "soap", "soap", "soap", "soap", "soap", "soap", "soap"
), var_3 = c("C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C"), var_4 = c(988.856158671407, 1000.23888564896, 997.039675082569, 
1001.09291198164, 1002.98988896299, 1005.41677956183, 997.170442654021, 
1008.16418133488, 990.169419475989, 1002.11962397764, 1000.65881048338, 
1000.6255556549, 996.706428965879, 993.887084764435, 1000.5277212491, 
997.595615809563, 992.846778862404, 1000.37249544151, 999.435545324196, 
995.477367164094, 1003.05859761336, 999.47812876303, 995.860251713007, 
995.632372813146, 999.770083493782), var_5 = c(994.468875098246, 
1003.96990710863, 996.401009591011, 1005.87900720863, 1003.10043497883, 
1006.86178446511, 1000.16218228559, 1014.54963756727, 994.616732970479, 
1004.23167123533, 1002.37269462739, 1001.87516303241, 997.778855524445, 
996.483722875524, 1001.1303870722, 998.160790249582, 990.769696036975, 
1000.45401759063, 1002.01859674451, 998.79253832661, 1004.13493084409, 
1000.54459607728, 997.719615789644, 999.380020106129, 1002.62315679074
), var_6 = c(1012.10534266625, 1015.40462298842, 999.9107434841, 
1014.38627383064, 1008.93405130319, 1015.18680921429, 1010.14219845841, 
1020.70520656907, 1007.5072983389, 1010.76932384644, 1007.87875085817, 
1005.48278706243, 1003.35777109534, 1003.47085321062, 1007.33985613752, 
1007.25872117706, 999.112164307972, 1002.29939831067, 1003.07417333636, 
1003.85793371694, 1009.53278894638, 1011.19644538506, 1011.23395633535, 
1007.39260082121, 1013.65590374333), var_7 = c(990.850727928741, 
993.398346143465, 1002.54036572775, 997.364309124077, 1002.08056674659, 
1005.06669915366, 1004.31923128858, 1003.23718800791, 994.268259914595, 
1000.67737011882, 999.619189546307, 1002.68202480584, 999.041146242958, 
993.414424263201, 1000.59471576055, 1000.19683225081, 1000.16517157422, 
997.540443383467, 1001.84917352127, 1001.07658425743, 1004.40588599635, 
998.823414049946, 997.858229406642, 994.87259980467, 998.785952807378
), var_8 = c(994.890815628412, 996.851278420874, 1002.1043798945, 
1005.81171180245, 1002.87987510596, 1007.79412746756, 1009.50695673265, 
1008.01447499455, 999.512821404105, 1004.2013984755, 1000.81528330285, 
1004.35199943309, 1002.11976068962, 997.40997781305, 1001.88805943989, 
1002.594272601, 998.560175268731, 998.135153010693, 1004.4252040573, 
1004.73673205744, 1005.71745745982, 1001.17048429098, 999.360169227143, 
997.384140934815, 1001.00287124848), var_9 = c(1009.51261484649, 
1005.02619159395, 1005.49719893791, 1010.84145164945, 1004.68478802971, 
1016.32418136177, 1014.69013052771, 1012.07680708286, 1012.41952646586, 
1010.56312371765, 1004.28046277054, 1007.64737651417, 1007.08645126073, 
1003.27924738048, 1005.58288092655, 1011.72595267599, 1003.49843659402, 
1001.33918111569, 1003.15289777247, 1007.05337876271, 1007.60523861932, 
1008.12060882554, 1009.93070913311, 1002.02262615479, 1010.11935930848
), var_10 = c(995.608170991922, 998.216388150646, 1001.60211665164, 
999.832076213262, 1001.40362599894, 1008.65746780516, 1005.14963479715, 
1005.69883686071, 995.36726126399, 1001.98356248343, 1002.52938643378, 
1004.44686315697, 1001.88437037401, 992.528325989138, 1000.17011585976, 
1002.59521869899, 1007.04880390672, 998.313461257726, 1000.28328142721, 
1004.36911508867, 1002.78021097217, 996.747503975562, 999.187033093333, 
996.024445680514, 999.229331486915), var_11 = c(999.349990537239, 
1000.25571835554, 1001.09116707486, 1007.06029312947, 1002.72729847885, 
1009.68783611392, 1009.23934223585, 1008.95767573724, 999.644079501528, 
1004.46546567977, 1003.02810334276, 1005.67690071529, 1004.36701185857, 
996.291832791975, 1002.04095164098, 1005.51311465032, 1005.69841082672, 
998.788899712536, 1003.03620977913, 1006.88800630791, 1003.89090518079, 
998.081210763667, 1000.05747905747, 998.094784527214, 1001.67927888372
), var_12 = c(1011.12126521731, 1004.59413830322, 1004.1077131243, 
1010.13326381032, 1002.86650202467, 1016.25544969107, 1012.00856171947, 
1011.06711242009, 1010.68633713312, 1009.10677414828, 1004.92332669674, 
1007.96701055541, 1006.81338213344, 1000.30733607564, 1005.31077105909, 
1010.90426287755, 1007.22349662969, 1001.6706218772, 1001.669317131, 
1007.40312084436, 1004.96827359204, 1003.52588208615, 1007.35889570568, 
1001.24372245315, 1008.83562199571), var_13 = c(996.838989582336, 
1002.27800090558, 1001.75290674649, 1004.7636391085, 1000.97339668679, 
1012.11738595707, 1003.17906097432, 1004.61956745266, 998.954167698171, 
1000.33718227001, 1002.91357065339, 1003.65320446801, 998.795991193962, 
995.260177765719, 1002.5924005443, 1003.26725194424, 1010.94382735439, 
999.908089656425, 996.491714680007, 1003.35958383952, 1000.1980165471, 
996.129597532203, 998.068498757686, 995.940627650744, 1004.06645814493
), var_14 = c(997.870867692911, 1003.02403433836, 1000.66826545719, 
1006.7506344557, 1001.15587576193, 1010.51785193511, 1004.99936090838, 
1004.57223746317, 1000.36408792936, 1000.58925031439, 1001.3954846972, 
1003.55543459632, 1000.99816870794, 997.52989338229, 1004.17893812675, 
1005.94481009989, 1007.51898916194, 999.52752418934, 1000.41259599596, 
1004.95053154685, 1001.27825568282, 997.249111602143, 997.60258568041, 
996.624936012322, 1005.66199164861), var_15 = c(1005.24535230967, 
1002.99896314, 1000.43007709819, 1007.20593389744, 998.711140983915, 
1013.30601537204, 1005.57921718108, 1005.24004334238, 1005.96824937368, 
1001.62293479253, 1002.21921258547, 1004.07313151834, 1000.71338636323, 
997.083115696827, 1004.8346929062, 1006.3003345886, 1007.02302307691, 
1003.2273466626, 998.987599433243, 1004.20438929142, 1002.0228708307, 
1001.34241259142, 1002.11369999794, 996.717876200798, 1008.01351241284
)), row.names = c(NA, -25L), class = c("tbl_df", "tbl", "data.frame"
))

我想通过变量合并它们:

var_1
var_2

并将它们具有的与原始数据集相同的顺序保持在最终合并数据集中,其中所有变量的顺序与它们被排序时相同,即:

var_1 
var_2 
var_3 (all three levels column coming from the three different datasets one after the other); 
var_4 = same order as for var_3; 
var_5 = same as above; 
var_5 = same as the three ones above;

我的问题是如何通过使用dplyr %〉%操作符在一个命令中完成所有这些操作。然而,如果你不知道如何解决这个问题,你可以建议的每一种方法都将受到赞赏。

zbdgwd5y

zbdgwd5y1#

好吧,我不能在一个管道中工作(也许其他人可以,可能用一些奇怪的purrr魔法),但这个也可以做到这一点。它基本上首先获得var名称,然后将它们重新排列成你想要的顺序,然后将它们重新定位在连接的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 ...

另一种解决方案是(假设所有三个数据集都是相同的,正确的顺序)创建一个循环,选择前三列,然后从df 1中选择第4列,从df 2中选择第4列,从df 3中选择第4列,然后每个数据集的第5列,然后第6列,依此类推。
根据TO的评论进行更新:
如果我不想把所指出的变量一个放在另一个旁边,而是全部放在一个具有单个名称的列中(即COND列应包含COND和COND.y以下的值,以及COND.x以下的值,并且其他列中的相应值也是如此),那么代码应该如何重塑?
我不能100%确定哪种解决方案更适合您的目的,但这里有两种不同的分组/排序:
A)我们按行绑定数据,这样第一行来自df 1,第二行来自df 2,以此类推。这仍然保留了所有其他列。

df_join <- df1 %>%
  add_row(df2) %>%
  add_row(df3) %>%
  arrange(ID)

B)我们可以pivot_longer,这样“真实的”变量也会在彼此之下。你需要一个基于变量名称列的额外排序变量:

df_join %>%
  mutate(data_ID = c(rep(1, nrow(df1)),
                     rep(2, nrow(df2)),
                     rep(3, nrow(df3))),
         .before = 1) %>%
  pivot_longer(cols = -c(data_ID, ID, GR, SES, COND)) %>%
  group_by(data_ID) %>%
  mutate(vars_sort = 1:n()) %>%
  ungroup() %>%
  arrange(vars_sort)

# A tibble: 900 x 8
   data_ID ID    GR    SES   COND    name                    value vars_sort
     <dbl> <chr> <chr> <chr> <chr>   <chr>                   <dbl>     <int>
 1       1 01    RP    V     NEG-CTR P3(400-450).FCz       -11.6           1
 2       2 01    RP    V     NEU-NOC P3(400-450).FCz        -4.00          1
 3       3 01    RP    V     NEG-NOC P3(400-450).FCz       -11.1           1
 4       1 01    RP    V     NEG-CTR P3(400-450).Cz         -5.17          2
 5       2 01    RP    V     NEU-NOC P3(400-450).Cz          0.622         2
 6       3 01    RP    V     NEG-NOC P3(400-450).Cz         -5.53          2
 7       1 01    RP    V     NEG-CTR P3(400-450).Pz         11.9           3
 8       2 01    RP    V     NEU-NOC P3(400-450).Pz         13.0           3
 9       3 01    RP    V     NEG-NOC P3(400-450).Pz         12.1           3
10       1 01    RP    V     NEG-CTR LPPearly(500-700).FCz -11.8           4
# ... with 890 more rows

相关问题