R - 根据列和列名减去 df 元素

R - subtract df elements based on column and colnames

我有两个 dfs - 基准 return 和 allreturns(下面的示例),我想创建第三个 df,returns 减去值 allreturns-benchmarkreturns,匹配allreturns每一行的"startmonth"值,以及两者一致的colname(V1:V12) dfs.

这个问题类似于在 Excel 中有两个条件的索引匹配,我在行端按 startmonth 匹配,在列端按 "V month" 匹配(此处为列名)。

例如,如果在所有returns 中我有一个 2005 年 1 月的开始月份,并且 V3 中该行的值为 5,基准return 为 2005 年 1 月行和 V3 列为 3,则新 df 中所有 return 行的值将为 3。依此类推。

实际的 allreturns dataframe 相当大,所以我担心在减去之前合并,并且想知道这是否是最可行的方法,或者我是否可以一步减去元素。

感谢您的帮助!

全部return个:

structure(list(startmonth = structure(c(2012, 2012.08333333333, 
2012.16666666667, 2012.25, 2012.33333333333, 2012.41666666667, 
2012.5, 2012.58333333333, 2012.66666666667, 2012.75), class = "yearmon"), 
    V1 = c(0.0284247303, 0.0010087300099999, 0.0022169700300001, 
    -0.01468004942, 0.00102045998000011, 0.00591265976000011, 
    0.0199267303899999, 0.0417483615900001, 0.00335606009, 0.00334482998000007
    ), V2 = c(0.0294621331884797, 0.00322793636420049, -0.012495624619603, 
    -0.0136745698429375, 0.0069391533726606, 0.0259572101269254, 
    0.0625070003256281, 0.0452445316901551, 0.00671211552040374, 
    0.0314807661806089), V3 = c(0.0317444198847785, -0.0114994993211506, 
    -0.0114879159244523, -0.00784276316178301, 0.0270041584010525, 
    0.0687892427111718, 0.0660728376647666, 0.0487406969361834, 
    0.0349424776135903, 0.0430893809414579), V4 = c(0.0165983608120608, 
    -0.0104907741199978, -0.00564318030266497, 0.0119276866011795, 
    0.0698798993604133, 0.0723761636332563, 0.0696386700530516, 
    0.0781496303938529, 0.0465900515831383, 0.0514300069951692
    ), V5 = c(0.0176357587550031, -0.00464014273798818, 0.0141710999549016, 
    0.0541740095643377, 0.0734704805917499, 0.0759630795752142, 
    0.0996336273987892, 0.090283471493128, 0.054958669279074, 
    0.0744177901533509), V6 = c(0.0236526927561311, 0.0151941247787009, 
    0.056511081749947, 0.057711880885752, 0.0770610568378784, 
    0.106135386898253, 0.112009256278245, 0.099001465266642, 
    0.0780236008188706, 0.0925232124743112), V7 = c(0.0440507439776798, 
    0.0575768161840056, 0.0600567964260508, 0.0612497472951408, 
    0.107264153793045, 0.118584188660589, 0.120900971163225, 
    0.123029319908411, 0.0961897860250946, 0.0862168299189658
    ), V8 = c(0.0876381519555687, 0.06112610752891, 0.0636025061792393, 
    0.0910094612943682, 0.119725659059378, 0.127528477230367, 
    0.145407622386291, 0.141953913583335, 0.0898622388709431, 
    0.0799104502736558), V9 = c(0.0912883309497081, 0.0646753939459335, 
    0.0934281965725043, 0.103288031047544, 0.128679074917691, 
    0.152180027943971, 0.164709321337116, 0.135362201702614, 
    0.0835346946365931, 0.076045245502748), V10 = c(0.0949385048758928, 
    0.0945311704081673, 0.105733987546834, 0.11211001031858, 
    0.153355781552244, 0.171595851273483, 0.157986258190939, 
    0.128770492863592, 0.0796565180062141, 0.069524282329458), 
    V11 = c(0.125642923731963, 0.10684937460315, 0.114575524881519, 
    0.136424461680214, 0.172791417952441, 0.164833036942378, 
    0.151263198147072, 0.124730408712938, 0.0731136700886696, 
    0.0680519003357034), V12 = c(0.138311269558968, 0.115699830661878, 
    0.138943880653118, 0.155574781977547, 0.166021702439959, 
    0.158070225731925, 0.147142608328795, 0.117914407614703, 
    0.0716363466924688, 0.086773098425986)), .Names = c("startmonth", 
"V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", 
"V11", "V12"), class = "data.frame", row.names = c(NA, 10L))

基准 returns:

    structure(list(startmonth = structure(c(2005, 2005.08333333333, 
2005.16666666667, 2005.25, 2005.33333333333, 2005.41666666667, 
2005.5, 2005.58333333333, 2005.66666666667, 2005.75, 2005.83333333333, 
2005.91666666667, 2006, 2006.08333333333, 2006.16666666667, 2006.25, 
2006.33333333333, 2006.41666666667, 2006.5, 2006.58333333333, 
2006.66666666667, 2006.75, 2006.83333333333, 2006.91666666667, 
2007, 2007.08333333333, 2007.16666666667, 2007.25, 2007.33333333333, 
2007.41666666667, 2007.5, 2007.58333333333, 2007.66666666667, 
2007.75, 2007.83333333333, 2007.91666666667, 2008, 2008.08333333333, 
2008.16666666667, 2008.25, 2008.33333333333, 2008.41666666667, 
2008.5, 2008.58333333333, 2008.66666666667, 2008.75, 2008.83333333333, 
2008.91666666667, 2009, 2009.08333333333, 2009.16666666667, 2009.25, 
2009.33333333333, 2009.41666666667, 2009.5, 2009.58333333333, 
2009.66666666667, 2009.75, 2009.83333333333, 2009.91666666667, 
2010, 2010.08333333333, 2010.16666666667, 2010.25, 2010.33333333333, 
2010.41666666667, 2010.5, 2010.58333333333, 2010.66666666667, 
2010.75, 2010.83333333333, 2010.91666666667, 2011, 2011.08333333333, 
2011.16666666667, 2011.25, 2011.33333333333, 2011.41666666667, 
2011.5, 2011.58333333333, 2011.66666666667, 2011.75, 2011.83333333333, 
2011.91666666667, 2012, 2012.08333333333, 2012.16666666667, 2012.25, 
2012.33333333333, 2012.41666666667, 2012.5, 2012.58333333333, 
2012.66666666667, 2012.75, 2012.83333333333, 2012.91666666667, 
2013, 2013.08333333333, 2013.16666666667, 2013.25, 2013.33333333333, 
2013.41666666667, 2013.5, 2013.58333333333, 2013.66666666667, 
2013.75, 2013.83333333333, 2013.91666666667, 2014, 2014.08333333333, 
2014.16666666667, 2014.25, 2014.33333333333, 2014.41666666667, 
2014.5, 2014.58333333333, 2014.66666666667, 2014.75, 2014.83333333333
), class = "yearmon"), V1 = c(0.83740290789865, 0.175849379040301, 
0.115745179820806, 0.588703967630863, 0.663440374424681, 0.122854199260473, 
0.329111734172329, 0.386456901207566, 0.328031520592049, 0.91476626554504, 
0.838957223575562, 0.482753940392286, 0.203118846053258, 0.832898694789037, 
0.569874424720183, 0.85476457583718, 0.873173910891637, 0.599100088467821, 
0.990275882417336, 0.360726963961497, 0.366468257969245, 0.213221889222041, 
0.736634878907353, 0.770827260101214, 0.78438491653651, 0.443004491971806, 
0.0431681957561523, 0.362984816078097, 0.933778994018212, 0.890268561197445, 
0.0288882539607584, 0.704298130935058, 0.0533608365803957, 0.965311451349407, 
0.797033908311278, 0.702818523859605, 0.615030416054651, 0.783928481163457, 
0.0931849081534892, 0.693762929877266, 0.709550492931157, 0.527024435112253, 
0.463098735082895, 0.975416449131444, 0.418448116164654, 0.716492718551308, 
0.900618054671213, 0.937315494287759, 0.371745373355225, 0.876447483897209, 
0.638362225377932, 0.230460224673152, 0.84510417538695, 0.709547182312235, 
0.135460949270055, 0.86736886552535, 0.0408105747774243, 0.59634031355381, 
0.378888477338478, 0.400274467887357, 0.73448179056868, 0.0201477019581944, 
0.786700234282762, 0.145882265642285, 0.722347423899919, 0.495645943330601, 
0.8204965388868, 0.944398637628183, 0.920839294558391, 0.244003586936742, 
0.770501866471022, 0.608119423966855, 0.599260926479474, 0.316019677789882, 
0.594015010166913, 0.862004647962749, 0.248981321696192, 0.251351157668978, 
0.86709617683664, 0.477806584443897, 0.308428246993572, 0.930116105359048, 
0.820579398889095, 0.407806032337248, 0.00576947652734816, 0.205415812321007, 
0.503058285918087, 0.421452875249088, 0.171408428112045, 0.927357386332005, 
0.736080843256786, 0.0634949475061148, 0.628076422959566, 0.825837341602892, 
0.860992948058993, 0.637153514893726, 0.496678973315284, 0.532001776387915, 
0.742080342955887, 0.279888727236539, 0.702802252722904, 0.20291490922682, 
0.761654698755592, 0.413414426380768, 0.0555722850840539, 0.125917585333809, 
0.545490033458918, 0.19256273820065, 0.242046363884583, 0.0174444266594946, 
0.380734569625929, 0.735217718640342, 0.346396475099027, 0.997578310314566, 
0.544119880301878, 0.610004446236417, 0.643721028929576, 0.0819557739887387, 
0.666767429094762), V2 = c(0.325795059790835, 0.0280552506446838, 
0.563847373938188, 0.968374367104843, 0.0397681496106088, 0.409115779446438, 
0.568448322825134, 0.498488057404757, 0.725102471420541, 0.745882571907714, 
0.956671225838363, 0.732607456855476, 0.723114193649963, 0.715586860431358, 
0.740428047254682, 0.447626604698598, 0.136078626848757, 0.343137193005532, 
0.994878244586289, 0.569102957844734, 0.264083825284615, 0.0172849236987531, 
0.902412769151852, 0.237230452476069, 0.342157111503184, 0.940635218983516, 
0.355561858741567, 0.0913543966598809, 0.730372012127191, 0.424820594489574, 
0.862009244738147, 0.673514985479414, 0.0558020728640258, 0.769041493767872, 
0.429111589910462, 0.814037810778245, 0.485316230216995, 0.407698386581615, 
0.0245183343067765, 0.38149883877486, 0.939248957671225, 0.206260631326586, 
0.773903579683974, 0.955869939411059, 0.930366629268974, 0.497170565184206, 
0.232823575846851, 0.119068749016151, 0.188956700265408, 0.769491335609928, 
0.487886165967211, 0.249603169038892, 0.61157290590927, 0.967520525446162, 
0.669223197968677, 0.0509379308205098, 0.630447809584439, 0.93567297863774, 
0.296445045853034, 0.231306056724861, 0.664361900882795, 0.570044811815023, 
0.988849612651393, 0.96225642086938, 0.567738547222689, 0.562166472664103, 
0.382391084451228, 0.87047664122656, 0.553916300181299, 0.348980325739831, 
0.861658359179273, 0.824328954331577, 0.602889677742496, 0.108061951119453, 
0.510826006531715, 0.787226195679978, 0.209260584786534, 0.467020642245188, 
0.916463258676231, 0.810527657391503, 0.259206298971549, 0.383183149620891, 
0.399636579910293, 0.517378240358084, 0.277383098145947, 0.0540329110808671, 
0.334274391178042, 0.762725146021694, 0.624413190409541, 0.664978531654924, 
0.877504705451429, 0.133810454979539, 0.303127287654206, 0.401904000202194, 
0.333468962227926, 0.880315525457263, 0.693340804660693, 0.104616170749068, 
0.726646555587649, 0.787352371029556, 0.805738570401445, 0.842183391330764, 
0.203002500347793, 0.4008568006102, 0.764609909849241, 0.68458701716736, 
0.826388106215745, 0.458343503531069, 0.681572974193841, 0.816049969289452, 
0.99422895279713, 0.586194961098954, 0.106009811395779, 0.564460329711437, 
0.763485615840182, 0.641962635098025, 0.556110577424988, 0.624630474718288, 
0.0865604616701603), V3 = c(0.103272917214781, 0.145110326353461, 
0.139097063103691, 0.704820184968412, 0.499401112552732, 0.183894460555166, 
0.142426387872547, 0.654179619625211, 0.231400778284296, 0.07779724849388, 
0.639861593721434, 0.329660887364298, 0.207785484148189, 0.349164958344772, 
0.918049042113125, 0.561889169970527, 0.801518984371796, 0.374053293140605, 
0.279966498259455, 0.211821462959051, 0.988277795957401, 0.208132355008274, 
0.358667319407687, 0.489217024296522, 0.235113901318982, 0.0665953925345093, 
0.682081600185484, 0.923956993501633, 0.951206840341911, 0.153028624830768, 
0.457171397982165, 0.497927478514612, 0.464426794555038, 0.700641778064892, 
0.350401868345216, 0.239867100957781, 0.495585883036256, 0.659481293288991, 
0.121178397443146, 0.966434765374288, 0.781230272026733, 0.191542416810989, 
0.144150232896209, 0.649679786758497, 0.990280882688239, 0.182715938193724, 
0.990455492865294, 0.0560185567010194, 0.258451806847006, 0.745103346649557, 
0.389899141388014, 0.0482800761237741, 0.0293514989316463, 0.393075271975249, 
0.53582366858609, 0.522070667007938, 0.802251521963626, 0.292497304268181, 
0.523711683694273, 0.606568831950426, 0.198962268885225, 0.391696797916666, 
0.059201990487054, 0.0645288161467761, 0.808837213553488, 0.0917617143131793, 
0.698996715713292, 0.330968038411811, 0.843029006384313, 0.9479373190552, 
0.514315341366455, 0.325804576743394, 0.3519700136967, 0.537765094777569, 
0.871437344467267, 0.667971923248842, 0.0599088978487998, 0.560071154031903, 
0.902071484364569, 0.193852530093864, 0.629741749959067, 0.0646166354417801, 
0.238403898198158, 0.737684201914817, 0.0984734564553946, 0.314926577033475, 
0.667433348251507, 0.200710391392931, 0.510746134677902, 0.0941306182648987, 
0.408070910954848, 0.349233734188601, 0.0384121341630816, 0.469132867408916, 
0.968564240029082, 0.250593126285821, 0.0388190811499953, 0.301829227479175, 
0.62604419933632, 0.981997888535261, 0.699610964162275, 0.319871086394414, 
0.0948076455388218, 0.0173410377465189, 0.733540053712204, 0.304378144675866, 
0.593588132876903, 0.85377266141586, 0.272565411170945, 0.151829193811864, 
0.951305341674015, 0.21787527250126, 0.728856807108968, 0.932554795406759, 
0.530237913830206, 0.146221859846264, 0.220554714323953, 0.133143884129822, 
0.845931079704314), V4 = c(0.828090658877045, 0.244675925467163, 
0.617208154639229, 0.675795117858797, 0.921914914855734, 0.777150499867275, 
0.841221024747938, 0.492060288786888, 0.326705329120159, 0.686669388087466, 
0.121503615053371, 0.367948720930144, 0.106932319700718, 0.363004767335951, 
0.350841429783031, 0.204824617831036, 0.233977065887302, 0.7365177560132, 
0.0454314323142171, 0.865328938700259, 0.328468273859471, 0.187198075000197, 
0.0820242392364889, 0.516697632148862, 0.481362534221262, 0.620970563730225, 
0.817455855663866, 0.98652182915248, 0.980461026541889, 0.0709870441351086, 
0.536431754939258, 0.976550402119756, 0.40865416219458, 0.890750366030261, 
0.662430537166074, 0.388162778690457, 0.959246737649664, 0.931342099327594, 
0.770709854317829, 0.279344801558182, 0.305849055293947, 0.370182184502482, 
0.562077804701403, 0.240711282240227, 0.630282753147185, 0.170173112303019, 
0.313635889207944, 0.694733579177409, 0.319224840262905, 0.34643409284763, 
0.695703814737499, 0.39429243397899, 0.189475272549316, 0.516221221769229, 
0.777665859088302, 0.558720602886751, 0.229818729683757, 0.484828221844509, 
0.481090292567387, 0.0106754100415856, 0.684031964046881, 0.815850237151608, 
0.484693359583616, 0.362359361024573, 0.116397734964266, 0.350968763930723, 
0.150200203061104, 0.91834011208266, 0.52454294054769, 0.148975162534043, 
0.510950653115287, 0.803136837668717, 0.918003916973248, 0.100692971842363, 
0.0494276131503284, 0.526557822246104, 0.0758368712849915, 0.269769576610997, 
0.101892914390191, 0.424805645598099, 0.110284168738872, 0.709118485450745, 
0.784806538838893, 0.0519805552903563, 0.398219058057293, 0.34969964553602, 
0.695627773646265, 0.549499853979796, 0.273849666584283, 0.151610748609528, 
0.874867760110646, 0.198461376130581, 0.569974714424461, 0.725238451501355, 
0.594061649870127, 0.287728592054918, 0.167462798533961, 0.126261120429263, 
0.557413443457335, 0.464988547144458, 0.552185143576935, 0.0831529255956411, 
0.333197562023997, 0.574352362193167, 0.4974719658494, 0.190328923054039, 
0.82773098279722, 0.356611662777141, 0.803166383411735, 0.320444498909637, 
0.366018867818639, 0.809054122306406, 0.553693874971941, 0.293376821791753, 
0.97094266442582, 0.879881211323664, 0.308937143767253, 0.579827836947516, 
0.496185180498287), V5 = c(0.923095905454829, 0.762332424288616, 
0.972339176572859, 0.422411200823262, 0.940925979521126, 0.540574067272246, 
0.985336700454354, 0.8358859855216, 0.452674505067989, 0.919172912370414, 
0.177077513653785, 0.484894159948453, 0.228302743285894, 0.588133673649281, 
0.120827580103651, 0.320581880165264, 0.132633685600013, 0.244240331463516, 
0.0881568337790668, 0.0182364885695279, 0.619532398879528, 0.763255036668852, 
0.360307943308726, 0.291531737195328, 0.637159892357886, 0.423571469495073, 
0.144552590325475, 0.232900225091726, 0.431558183860034, 0.624517237534747, 
0.660041945520788, 0.0605304769705981, 0.894808513810858, 0.766446133842692, 
0.708146936027333, 0.980518834665418, 0.831859190016985, 0.63390138419345, 
0.0906804192345589, 0.863758355844766, 0.81235816469416, 0.754735963186249, 
0.96086330129765, 0.379362520063296, 0.86472422350198, 0.434156836709008, 
0.921033434569836, 0.930532508296892, 0.919923858484253, 0.0185073432512581, 
0.190673178527504, 0.389489938504994, 0.251335079083219, 0.398261951981112, 
0.323421294335276, 0.0406879091169685, 0.0622786141466349, 0.511600321624428, 
0.761615104507655, 0.267784109571949, 0.593046235619113, 0.923055295599625, 
0.481184788281098, 0.539936339249834, 0.71660251962021, 0.525291009107605, 
0.878714059945196, 0.127967000938952, 0.616779315285385, 0.174148306716233, 
0.737354298587888, 0.931568023283035, 0.51300498447381, 0.860497320536524, 
0.776048321742564, 0.0440801647491753, 0.0390385524369776, 0.765144015429541, 
0.334515287308022, 0.312304191756994, 0.874887532787398, 0.495062640635297, 
0.772741753375158, 0.847717159660533, 0.465354095678777, 0.988151066936553, 
0.933549190638587, 0.493211443535984, 0.604139760835096, 0.11511691333726, 
0.955369158880785, 0.00772590702399611, 0.396924669155851, 0.360593063989654, 
0.44654577318579, 0.775453717214987, 0.103126123780385, 0.0639552725479007, 
0.382581982528791, 0.221267726272345, 0.569829758256674, 0.189883300568908, 
0.343288340838626, 0.372111623873934, 0.235726190032437, 0.540518779074773, 
0.706296919612214, 0.939652142813429, 0.0611454977188259, 0.428340997546911, 
0.625002122484148, 0.828457030234858, 0.038334577344358, 0.986632968764752, 
0.838095314567909, 0.635932896286249, 0.375527458963916, 0.63829215336591, 
0.54482639557682), V6 = c(0.370218734024093, 0.419946043053642, 
0.545280206250027, 0.277894535567611, 0.00736652850173414, 0.790516802109778, 
0.313465759390965, 0.809552269754931, 0.842788263224065, 0.499014808563516, 
0.623795239022002, 0.548406617250293, 0.05658471561037, 0.965675859013572, 
0.472378655336797, 0.524981135968119, 0.46187190245837, 0.387807991588488, 
0.938760220305994, 0.50433289213106, 0.233549498720095, 0.00805983133614063, 
0.127320852363482, 0.672702707583085, 0.415367575129494, 0.250937715405598, 
0.0297437864355743, 0.406187633285299, 0.675050344318151, 0.292069549206644, 
0.61900219717063, 0.616054858779535, 0.250656338408589, 0.518912112340331, 
0.183584470301867, 0.0910171526484191, 0.546375629259273, 0.621274302946404, 
0.0846014332491904, 0.621578456601128, 0.576011084485799, 0.504746064078063, 
0.221732606412843, 0.69746913574636, 0.164020692463964, 0.158075062790886, 
0.847464352613315, 0.605109791038558, 0.596407983452082, 0.0114535829052329, 
0.063888885313645, 0.353724629618227, 0.612920761108398, 0.957113454118371, 
0.741108618909493, 0.0766351209022105, 0.641778253018856, 0.988549496047199, 
0.554500735830516, 0.490220351610333, 0.814872513525188, 0.473424580646679, 
0.210200395900756, 0.70044357329607, 0.208990931045264, 0.680869682924822, 
0.932328240247443, 0.718588735442609, 0.658000691328198, 0.185951039893553, 
0.753725055372342, 0.123452978907153, 0.212786188349128, 0.228114538360387, 
0.00365104852244258, 0.814572377130389, 0.124759285245091, 0.11509175482206, 
0.3558327362407, 0.487612128257751, 0.344766818918288, 0.25334310438484, 
0.712012055562809, 0.453843667870387, 0.938410549657419, 0.718660024693236, 
0.325095203239471, 0.156200079247355, 0.175217281095684, 0.551201074384153, 
0.830405764747411, 0.281535709509626, 0.403883191291243, 0.545596988638863, 
0.282760329078883, 0.755758260609582, 0.310922948876396, 0.819937098305672, 
0.0865648619364947, 0.936576428823173, 0.880421161418781, 0.454629295505583, 
0.14402421656996, 0.840076276101172, 0.655401981435716, 0.569122347747907, 
0.438042796216905, 0.0409106772858649, 0.944646059069782, 0.825017294846475, 
0.0786613943055272, 0.0726817650720477, 0.588666438357905, 0.588839216856286, 
0.444881158880889, 0.721039973665029, 0.534136849688366, 0.852300873491913, 
0.281278134556487), V7 = c(0.141886763740331, 0.532325896434486, 
0.588067500153556, 0.640411838656291, 0.946903814794496, 0.0120973803568631, 
0.952789547387511, 0.107461815234274, 0.236731971381232, 0.79851196357049, 
0.312241489300504, 0.458273995434865, 0.275089886970818, 0.966928232926875, 
0.153488726355135, 0.250537436921149, 0.262579692527652, 0.622048743302003, 
0.497019267408177, 0.081929708365351, 0.995605900418013, 0.385106679983437, 
0.176982956239954, 0.696908152429387, 0.732609438244253, 0.187662536511198, 
0.936794911511242, 0.168030542787164, 0.569703943328932, 0.604032611008734, 
0.115415274398401, 0.807121691294014, 0.139127567177638, 0.530566544737667, 
0.27088794275187, 0.702946513192728, 0.755043589742854, 0.811690449714661, 
0.114138029748574, 0.233510547783226, 0.654041203204542, 0.441771358018741, 
0.969852354610339, 0.964909290196374, 0.541466456372291, 0.0193931085523218, 
0.201263489434496, 0.33053227327764, 0.687692743493244, 0.18348641670309, 
0.418929822510108, 0.383909322321415, 0.137825107900426, 0.217210126807913, 
0.453193115768954, 0.0340990910772234, 0.61968918191269, 0.780179363442585, 
0.80423841974698, 0.491154700284824, 0.351554640568793, 0.587180010043085, 
0.239602132700384, 0.644263347610831, 0.969654035987332, 0.547199918190017, 
0.397612665314227, 0.722763631725684, 0.72788112401031, 0.795322017977014, 
0.686627405928448, 0.762283272109926, 0.926369941327721, 0.894270734628662, 
0.257203514222056, 0.0720642674714327, 0.985171274049208, 0.660412719240412, 
0.648350277217105, 0.917640027124435, 0.694397774524987, 0.917451658984646, 
0.524428785312921, 0.0992080934811383, 0.100496702594683, 0.975102295167744, 
0.492288384120911, 0.0227630862500519, 0.813120178645477, 0.71656278334558, 
0.913717438234016, 0.636773794423789, 0.929305002791807, 0.892870238050818, 
0.780625773593783, 0.850657029775903, 0.204829484922811, 0.640264326473698, 
0.663275289349258, 0.789437669329345, 0.485021750209853, 0.0749609414488077, 
0.655508011812344, 0.74328700476326, 0.31854355102405, 0.0728525989688933, 
0.382023223210126, 0.215404908405617, 0.656497932272032, 0.92760404618457, 
0.781216817675158, 0.833905356237665, 0.361858946271241, 0.300276404013857, 
0.373293370939791, 0.853469310561195, 0.133900812361389, 0.986530470428988, 
0.411433706991374), V8 = c(0.849042785586789, 0.539557616924867, 
0.564340554876253, 0.755421123001724, 0.984947801567614, 0.112984226550907, 
0.403615512186661, 0.0420174030587077, 0.611705861985683, 0.659787082113326, 
0.876130681717768, 0.948364004027098, 0.203974137781188, 0.309610587079078, 
0.984034060733393, 0.534302587388083, 0.971052328590304, 0.812090678606182, 
0.12758506112732, 0.4455144808162, 0.415974463103339, 0.518267513252795, 
0.0146076162345707, 0.24067388032563, 0.0458081206306815, 0.366667992202565, 
0.0854597128927708, 0.0380873722024262, 0.663318049395457, 0.873134173220024, 
0.715449047507718, 0.83272837777622, 0.269359193509445, 0.936196858529001, 
0.00233305594883859, 0.890992778819054, 0.980090971337631, 0.483554403064772, 
0.535345176234841, 0.51444540428929, 0.802370720077306, 0.772680272115394, 
0.102163553936407, 0.00971000618301332, 0.902426401618868, 0.880951991537586, 
0.284606313100085, 0.0439106288831681, 0.937477112514898, 0.912608936196193, 
0.998304838780314, 0.745025476673618, 0.751772192539647, 0.278778153471649, 
0.162150844698772, 0.837488571181893, 0.388283311389387, 0.335549421375617, 
0.650775108020753, 0.990717119770125, 0.0961552720982581, 0.302420736989006, 
0.276211500866339, 0.733167931437492, 0.164790137903765, 0.249713592929766, 
0.836560999508947, 0.692190924426541, 0.570303184678778, 0.984758368227631, 
0.328286996111274, 0.917134278221056, 0.612915431382135, 0.242480249842629, 
0.52148644789122, 0.660938010783866, 0.448608730221167, 0.691765318857506, 
0.341251585632563, 0.304311832180247, 0.0227175708860159, 0.149960547685623, 
0.17484575859271, 0.518439768115059, 0.639089794829488, 0.860548116965219, 
0.705641856649891, 0.673352806130424, 0.100467606680468, 0.570258957333863, 
0.706326137995347, 0.70817924500443, 0.453099914360791, 0.70276203728281, 
0.252404118655249, 0.598284177947789, 0.741140816593543, 0.806638022884727, 
0.720691111404449, 0.68312481045723, 0.973559587728232, 0.917065860237926, 
0.874346838332713, 0.143847105791792, 0.453837911831215, 0.569988392293453, 
0.692350469296798, 0.806599335046485, 0.456668916391209, 0.386287276633084, 
0.908880397910252, 0.793711767531931, 0.754064193693921, 0.533496839459985, 
0.615311589790508, 0.331158632645383, 0.11861584126018, 0.408010076498613, 
0.222291024168953), V9 = c(0.125224817777053, 0.914628089172766, 
0.377369399182498, 0.751476752338931, 0.0853602013085037, 0.846715328749269, 
0.561333758756518, 0.0429282214026898, 0.284403225872666, 0.285037216497585, 
0.604412623681128, 0.319880489027128, 0.91246378608048, 0.455940068000928, 
0.867939765099436, 0.425015810178593, 0.470896222162992, 0.224302776856348, 
0.36109847901389, 0.594786533853039, 0.642710687592626, 0.207564177224413, 
0.284501956542954, 0.732748613227159, 0.774612213019282, 0.11284792679362, 
0.956883376231417, 0.821359134977683, 0.846147159812972, 0.411850540433079, 
0.770936917047948, 0.496579384431243, 0.619339638156816, 0.177370799472556, 
0.239690004847944, 0.224350028671324, 0.0038589695468545, 0.179824534803629, 
0.719484498025849, 0.798693082761019, 0.842053556581959, 0.294362392276525, 
0.221064954530448, 0.898532920051366, 0.723221117863432, 0.863187251612544, 
0.0994312795810401, 0.361342326970771, 0.783163121435791, 0.502090321853757, 
0.13186371861957, 0.659234342398122, 0.390342378057539, 0.328385862288997, 
0.446345729287714, 0.507106031524017, 0.152638432569802, 0.445379047188908, 
0.304815052077174, 0.0760775990784168, 0.924889291869476, 0.0672700984869152, 
0.199834800790995, 0.0825146653223783, 0.0361869866028428, 0.543002652470022, 
0.972956772428006, 0.190790882101282, 0.376062947325408, 0.351586842909455, 
0.689982761396095, 0.241374674253166, 0.838397472165525, 0.934896029997617, 
0.468875553458929, 0.628862793324515, 0.431529769208282, 0.935133612947538, 
0.352268195245415, 0.830344556132331, 0.666667894460261, 0.905744371935725, 
0.7915226041805, 0.895240528741851, 0.584306629607454, 0.864065658533946, 
0.0452204917091876, 0.55241105845198, 0.101811402710155, 0.749172917334363, 
0.27551553142257, 0.83245463995263, 0.274472723482177, 0.257237296551466, 
0.959463604493067, 0.109898063354194, 0.971408878453076, 0.0386630205903202, 
0.975892154034227, 0.358955943025649, 0.399594583781436, 0.98816026840359, 
0.964564403751865, 0.385808907682076, 0.42681196751073, 0.346436691703275, 
0.723757655825466, 0.839694014983252, 0.847320703556761, 0.127265167189762, 
0.192201391793787, 0.113802545471117, 0.373555006459355, 0.84189727040939, 
0.515876488294452, 0.0691332728601992, 0.834895022911951, 0.19373700208962, 
0.921317730797455)), .Names = c("startmonth", "V1", "V2", "V3", 
"V4", "V5", "V6", "V7", "V8", "V9"), class = "data.frame", row.names = c(NA, 
119L))

这是我通过合并得到的结果,然后仅限于每个 df 的前两列的结果:

> merge(Benret, Allret, by=1)[c(1,2,3,11,12)]
   startmonth        V1.x       V2.x        V1.y         V2.y
1    Jan 2012 0.005769477 0.27738310  0.02842473  0.029462133
2    Feb 2012 0.205415812 0.05403291  0.00100873  0.003227936
3    Mar 2012 0.503058286 0.33427439  0.00221697 -0.012495625
4    Apr 2012 0.421452875 0.76272515 -0.01468005 -0.013674570
5    May 2012 0.171408428 0.62441319  0.00102046  0.006939153
6    Jun 2012 0.927357386 0.66497853  0.00591266  0.025957210
7    Jul 2012 0.736080843 0.87750471  0.01992673  0.062507000
8    Aug 2012 0.063494948 0.13381045  0.04174836  0.045244532
9    Sep 2012 0.628076423 0.30312729  0.00335606  0.006712116
10   Oct 2012 0.825837342 0.40190400  0.00334483  0.031480766

计算差异并省略输入:

res <- merge(Benret, Allret, by=1)[c(1,2,3,11,12)]
> res$diffV1 <- with(res, V1.x -  V1.y)
> res$diffV2 <- with(res, V2.x -  V2.y)

> res[ -(2:5) ]
   startmonth      diffV1     diffV2
1    Jan 2012 -0.02265525 0.24792096
2    Feb 2012  0.20440708 0.05080497
3    Mar 2012  0.50084132 0.34677002
4    Apr 2012  0.43613292 0.77639972
5    May 2012  0.17038797 0.61747404
6    Jun 2012  0.92144473 0.63902132
7    Jul 2012  0.71615411 0.81499771
8    Aug 2012  0.02174659 0.08856592
9    Sep 2012  0.62472036 0.29641517
10   Oct 2012  0.82249251 0.37042323