首页 > 解决方案 > 如何同时播放两个同名的df

问题描述

我正在计算属于两个不同数据帧但具有相同列名和相同长度的两行的时间?

for (i in names(TIV) {for(j in names(urban.exp)) { result <- c(result, TIV[i] * urban.exp[j])}}

TIV 是 26 个 obs 和 23 个变量 urban.exp 是 26 个 obs 和 197 个变量

但是,TIV 中的名称都是唯一的,并且在 urban.exp 中是重复的。我想将 urban.exp 中的每个 colnames(假设有 3 个)乘以 TIV 中的一个,我想要一个元素乘法)。

dput(TIV)

structure(list(ALB = c(0.000248174906521353, 0.000339169273748206, 0.000159632297757388, 0.000228630507732613, 0.000267866815239384, 0.000289021390436638, 0.000428600968670754, 0.000359599958169296, 0.000285798240881231, 0.000351193053463129, 0.000350073894209511, 0.000978402075126559, 0.000509607726186487, 0.000474370444647724, 0.0009013352987314, 0.000919698063539768, 0.000931573514218432, -6.02817288364908e-05, 0.00147380111581113, 0.000138068449732178, 7.13563367172184e-05, 0.000203140614523849, 0.000141125871924299, 0.000596973372585642, 0.000476357937555003, 0.0159934624248568 ), DZA = c(0.000268396126256534, 0.00041499462052619, 0.000775855329071962, 0.000247614710989086, 0.00025371478550621, 0.000270821107879502, 0.000843441012186421, 0.000284398589838246, 0.000234477520662322, 0.000304042023076104, 0.000306305994281338, 0.000911873116533145, 0.00433211883574542, 0.000287545845467517, 0.000840538271603211, -0.00320821964214054, 0.00475209613678138, -5.7290313092215e-05, 0.00213214430861042, 0.000114926712176887, 3.95917869655464e-05, 0.000251224071825662, 9.26822881382479e-05, 0.000460120203572431, 0.000255400796998768, -0.000254677157762396), BGD = c(0.000913338803874099, 0.000686108490364341 , 0.000341395391373583, 0.000617209735009716, 0.00039797020138852, 0.000535263235754845, 0.000791566547477367, 0.000514028603231846, 0.000447276226487732, 0.000620114352054082, 0.000458591335459696, 0.00113114852677669, 0.009344425309504, 0.000630088174844855, 0.000959788528715684, 0.00104208072663453, 0.00117124840139069, -0.0469185795987902, 0.00158956762460143, 0.000217013947387598, 9.86773825322874e-05, 0.000271682582804807, 0.00023677199816467, 0.000831280500571276, 0.000555615271844585, 0.000942900765747189), BWA = c(0.000482117130379816, 0.000830244164580518, 0.0010254054258111, 0.000607072653315362, 0.000501231634671083, 0.000567616051110543, 0.000645457366742526, 0.000629605702268291, 0.000544685850129081, 0.000663044347955545, 0.000616545336715966, 0.0011139523841532, 0.00348953770692895, 0.000745614665488414, 0.00143510348232541, 0.00774019660647544, 0.00127052511998079, 0.000702994537318256, 0.00290565160847126, 0.000231931061991944 , 0.000139437569893227, 0.000501985666752962, 0.000265908463051288, 0.000924269112899131, 0.000593497744163397, 0.00148883147377942 ), BGR = c(0.000749598761828107, 0.000470535484932322, 0.000720674449220904, 0.000603227998691483, 0.000621004551337537, 0.000677626877413007, 0.000846521827869615, 0.00072421651063374, 0.000574760151566309, 0.000606340404903733, 0.000641753079513222, 0.00121667823080637, 0.00874076933500097, 0.000805997461660357, 0.00144446719326409, 0.00454146878811612, 0.00160946106966851, 0.000549577359307621, 0.0030412166447252, 0.000357055859646755, 0.000252394867645304, 0.000512263155725499, 0.000393777474739842, 0.000297806719907053, 0.000350807409141673, 0.00160360060359238), CHN = c(0.002208261, 0.001017573, 0, 0.007292695, 0.007154744, 0.004281515 , 0.01439357, 0.006233652, 0.024742643, 0.004137592, 0.012933709, 0.000567579, 0.001202401, 0.001177813, 0.001011665, 0.001888206, 0, 0.001511387, 0.014257881, 0.001691124, 0.002472086, 0.00090825, 0.008202734, 0, 0.000774492, -0.000511294), CRI = c(0.000173802315054641, 0.000254763819792943 , 0.00017095487110465, 0.000190916370461249, 0.000163013891471559, 0.00019023810268046, 0.000241830401487632, 0.000195528103604298, 0.000181694294574444, 0.00023970293344353, 0.000191302817493573, 0.000502674620550298, 0.000504081860272143, 0.000198365018149506, 0.000293652208460809, 0.000103385426910766, 8.79269368020482e-05, 0.000363590084937558, -0.000356578266563247, 0.000109963755347626, 6.0964616360274e-05, 0.000180258775514208, 0.00010363324527303, 0.000532655161887979, 0.000305765870009219, 0.000299797339793439), CIV = c( 0.000315874772473806, 0.000477073049291911, 0.000384174754204304, 0.000249719402871909, 0.000240229791946855, 0.000277924914654865, 0.000341162342372984, 0.000252520430814487, 0.000216558937346997, 0.000284311763635522, 0.000272649034343662, 0.000717120358981288, 0.00263743595952355, 0.000258461840061832, 0.000815425034976343, 0.000779303543601924, 0.000684210781801615, 0.0004345543613646, 0.00116728144643683, 0.000139655116666953, 7.98182805799198e-05, 0.000213312901529751, 0.00014912204987226, 0.000894440765688311, 0.000477558986104314, 0.000543283600887808 ), GEO = c(0.005279151, 0.001684205, 0.002129969, 0.00411568, 0.000915426, 0.000728686, 0.005128433, 0.000790025, 0.00103, 0.000677533, 0.000925394, 0, 0.012840032, 0.00127411, 0.000325425, 0.000322409, 0.000844766, 0.000493057, 0.006683474, 0.000532039, 0.013881833, 7.73e-05, 0.001214205, 0, 0.001580739, -5.84e-05 ), MYS = c(-0.0015130772816436, -0.0180260256928713, -0.00157505344574275, 0.000648534057061993, -0.00155740623112013, -0.00156283166303776, -0.00174340172748788, -0.00286197558369102, 0.00454689685232959, -0.0018938367416353, -0.00157663033130427, -0.0119022065132133, -0.00175299958373901, -0.0664537247250796, 0.0890100074952729, -0.00201299170182833, -0.00388584109571892, -0.00287411604410686, 0.00278277588580321, 0.00104715403241539, -0.00149602941092311, -0.0348283953747052, 0.00845292602307031, 0.201406414092222, -0.00159940992570112, 0.0522417938445839), MDV = c(0.000352490450642571, 0.000550328997212354, 0.000723764266026371, 0.000498694126592643, 0.000376656259071883, 0.000568444835699374, 0.000553226062140765, 0.000539125917393749, 0.000455725570252103, 0.000582716407592034, 0.000547111484301339, 0.000748961765363705, 0.00252751733647313, 0.000461514218724202, 0.00111914867302241, 0.00674965246948192, 0.000814018100861111, 0.000398847050156113, 0.00197233454579513, 0.000216093986731238, 0.000221521928466968, 0.000433770489564185, 0.000320690153287247, 0.000803622183313035, 0.000963639810417282, 0.00105293243540671), MEX = c(0.000944224, 0.000241484, 0.000316814, 0.000547203, 0.00143669, 0.001033885,0.003081825, 0.000896958, 0.002083657, 0, 0, 0, 0.006803516, 0.000501264, 0.000174385, 0.000142901, 0, 0, 0.010874537, 0.000616828, 0.001332897, 0.000293279, 0.002337931, 3.83e-06, 0, 6.46e-05), MNE = c (0.00012150207923852, 0.000477285337384979, 0.000160591667954874, 0.000168767228777349, 0.000230170002753131, 0.000199263460677769, 0.000141641421346877, 0.000191481985831653, 0.000166052527785696, 0.000233029831804151, 0.000242124674425282, 0.00044027771995825, 0.000189898429234294, 0.000188755929720485, 0.000493370655299545, 9.38389984119552e-05, 0.000123067692063739, 0.000185289145009044, 0.000100127504002848, 0.000140122390705789, 5.36773652866665e-05, 0.000126910780848532 , 0.000102783645088524, 0.000883716493977761, 0.000655194916948705, 0.000619806723692673), MAR = c(0.000576230588222724, 0.0090365, 0.0090365, 0.0090365, 0.0090365, 4000345251076888296, 0.000483401422068128, 0.000417547948938127, 0.000445795253675645, 0.000695651547697528, 0.000435693352572054, 0.000387010806414357, 0.000450117039837095, 0.000432861427345239, 0.00088113576907869, 0.00507307136732021, 0.00047563908243884, 0.000985012231911927, 0.00261731373390549, 0.00118859850891973, 0.000756818875820843, 0.00214454134266875, 0.00019073938090137, 0.000105582005170888, 0.000347913510249089, 0.000146312464918241, 0.000397294179428336, 0.000336379968259507, 0.00583520138552966 ), NAM = c(0.000445328582066306, 0.00068426871215696, 0.000537305638480289, 0.000407821878397232, 0.000438544612487918, 0.000448135959028999, 0.000449507784509406, 0.000667721676321114, 0.000445085615960187, 0.000537384415020835, 0.000564794265575911, 0.0010153798792818, 0.000393277394152737, 0.000510643021599451, 0.00167611519040259, 0.00181461346467337, 0.00173375774499516, 0.000404846140809306, 0.00295361961134029, 0.000283639921226707, 0.000129162928641247, 0.000328583967508361, 0.000213893132958698, 0.00112842286830381, 0.000808424289723378, 0.0017028605151185), ASM = c(0.000145277572623218, 0.000330744638372967, 0.000301739976711475, 0.000213198056186216, 0.000271157191143214, 0.000259969796260232, 0.000247505072371561, 0.000255936924306282, 0.000214221405273054, 0.00027795667828277, 0.000304520178949443 , 0.000451062121502298, 0.000393308980271543, 0.000114739885478548, 0.000608851923796505, 0.000843247013860957, 0.000599326495306679, 0.000252193741355666, 0.00138391529151655, 0.000127141687633323, 0.000182794659741862, 0.000288162870762742, 0.000246646241763648, 0.000381188885283242, 0.00058956932071467, 0.000381203990621746), ZAF = c(0, 0,0, 0.008086651, 0.003526301, 0.008866077, 0.008638975, 0.013957369, 0.018945326, 0.003328869, 0.019784222, 0, 0.023228398, 0.002659621, 0, 0.000469439, 0.000261756, 0.000677452, 0.002014355, 0.000494417, 0.00070504, 0.000241015, 0.000421291, 0, 0.000756811, -7.81e- 05), LKA = c(0.00032062899351777, 0.000163597655966227, 0.000164607201496355, 0.00022401980855161, 0.000185520448859277, 0.000214416338839827, 0.000303232936661481, 0.000220874598550123, 0.00019864469092968, 0.000279143124358859, 0.000237984611683141, 0.000411844500747206, 0.00193210615617892, 0.000237144260723747, 0.000670796981341913, 0.0010596649470119, 0.000970548396088071, 0.000447278000994621, 0.00170669404804761, 8.79527325411458e-05, 5.52875192092355e-05, 0.000151072122665778, 5.97154662557204e-05, 0.000100662627853025, 0.000123716763365379, -0.00204634871823),TUN = c(0.000444076147499441, 0.000610241398352655, 0.000656084832438002, 0.000439695762520918, 0.000426498556457417, 0.000473733195405614, 0.000723973432473194, 0.000472157924701055, 0.000410679108252149, 0.000487185316546125, 0.000467386957504676, 0.00113370594038486, 0.0049715366490553, 0.000492612136619973, 0.0010211452992161, -0.000731108197555239, 0.00116458699063456, -0.000359141563614933, 0.00182543211311049, 0.000210272293203948, 0.000129356195732316, 0.000394545538241387, 0.000251344539116984, 0.000946545872452504, 0.000536863794260094, 0.00187101573108743 ), UKR = c(0.004882587, 0, 0, 0.017154939, 0.003483648, 0.012498412, 0.033416735, 0.008278118, 0.021719782, 0.001383677, 0.013522355, 0, 0.004676683, 0.017151558, 0.002241068, 0, 0.001078673, 0, 0.00731783 , 0.001894759, 0.007383462, 0, 0.003917305, 0, 0.001327347, 0.001703462), VNM = c(0.012146918, 0.001980802, 0, 0.018360841, 0.009105785, 0.00451478, 0.011848205, 0.004563223, 0.005829405, 0.00522886, 0.003129561, 0, 0.015882704, 0.002322068, 0.000696516, 0.000524715, 0, 0.00234702, 0.052990123, 0.000458449, 0.007569633, 0 , 0.009779507, 0, 0.00104515, 0.000558566), ZMB = c(0.000185538679940368, 0.000228694546854409, 0.000383949747881978, 0.000254342140542114, 0.000232362262307685, 0.000266148738913681, 0.000511992931034977, 0.000259166340696073, 0.00026270606044175, 0.00035794076067972, 0.000292400223534108, 0.000619976846061065, 0.000330464208700282, 0.000265557476637801, 0.000711524137616014, 0.00116329601953602, 0.000799451656258591, 0.000249376561695306, 0.00155784130089073, 0.000122204936450822, 0.000117407505221938, 0.000191158548225592, 0.000154728626840974, 0.000254644598809.000216618820294215, -0.0582015964277787), ZWE = c(0.017749199607316, 0.0181301024162438, 0.0193645828015996, 0.0180011524832997, 0.0180772730678022, 0.0179081302943255, 0.0183263614878677, 0.0188799313913344, 0.0177187155287076, 0.0180093829576111, 0.0180562811976393, 0.0182044885084857, 0.0337412434812055, 0.0184421026669123, 0.0180483629775726, 0.0241177668870026, 0.0245657716836954, 0.024751860680692, 0.00663079658780753, 0.0176168200947908, 0.0185266072831957, 0.0176464697715503, 0.0176983948493923, 0.0181381302428299, 0.0180702616866683, 0.0180891094091982)), cNA.0.0180093829576111, 0.0180562811976393, 0.0182044885084857, 0.0337412434812055, 0.0184421026669123, 0.0180483629775726, 0.0241177668870026, 0.0245657716836954, 0.024751860680692, 0.00663079658780753, 0.0176168200947908, 0.0185266072831957, 0.0176464697715503, 0.0176983948493923, 0.0181381302428299, 0.0180702616866683, 0.0180891094091982)), class = "data.frame", row.names = c( NA, -26L))0.0180093829576111, 0.0180562811976393, 0.0182044885084857, 0.0337412434812055, 0.0184421026669123, 0.0180483629775726, 0.0241177668870026, 0.0245657716836954, 0.024751860680692, 0.00663079658780753, 0.0176168200947908, 0.0185266072831957, 0.0176464697715503, 0.0176983948493923, 0.0181381302428299, 0.0180702616866683, 0.0180891094091982)), class = "data.frame", row.names = c( NA, -26L))

标签: r

解决方案


You can isolate the columns directly and multiply them. Say the 3 TIV columns are column numbers 4, 7, 9 and the corresponding column numbers in urban.exe are 2, 11, 15 then

result <- TIV[ , c(4,7,9)] * urban.exp[ , c(2,11,15)]

Should give you the result you want.


推荐阅读