I have two databases, df1
and df2
. Therefore, I would like to multiply all the values of column M1
of df1
by the value corresponding to M1
of df2
dataset. Do this also for M2
, M3
and M4
. How to do this?
df1<-structure(list(M1 = c(7464.65871998132, 7464.65871998132,
7464.65871998132, 6828.03114705642, 6828.03114705642, 4283.33056756974,
4283.33056756974, 4283.33056756974, 4283.33056756974, 4283.33056756974,
2471.01407552018, 2471.01407552018, 1167.72213085951, 1759.83563490657,
2832.42425394852, 2216.93502047899, 2216.93502047899, 2216.93502047899,
2216.93502047899, 2216.93502047899, 2216.93502047899, 2216.93502047899,
2216.93502047899, 2216.93502047899, 2216.93502047899, 1382.78244210074,
1382.78244210074, 1382.78244210074, 1382.78244210074, 225.532020592308,
225.532020592308, 451.757369238554, 451.757369238554, 451.757369238554,
391.4333, 391.4333, 391.4333, 391.4333), M2 = c(0.717983998645269,
0.717983998645269, 0.717983998645269, 0.728999753129052, 0.728999753129052,
0.786965354287025, 0.786965354287025, 0.786965354287025, 0.786965354287025,
0.786965354287025, 0.850074652422915, 0.850074652422915, 0.915840124863607,
0.883139402208456, 0.835366766790403, 0.861211447058532, 0.861211447058532,
0.861211447058532, 0.861211447058532, 0.861211447058532, 0.861211447058532,
0.861211447058532, 0.861211447058532, 0.861211447058532, 0.861211447058532,
0.903336851097558, 0.903336851097558, 0.903336851097558, 0.903336851097558,
0.981191034215198, 0.981191034215198, 0.785270862340655, 0.785270862340655,
0.785270862340655, 0.804079828125457, 0.804079828125457, 0.804079828125457,
0.804079828125388), M3 = c(0.324701873490657, 0.324701873490657,
0.324701873490657, 0.348051447072024, 0.348051447072024, 0.528241884225376,
0.528241884225376, 0.528241884225376, 0.528241884225376, 0.528241884225376,
0.713363517626057, 0.713363517626057, 0.861245569166058, 0.792956247672247,
0.67415167385565, 0.741455966636511, 0.741455966636511, 0.741455966636511,
0.741455966636511, 0.741455966636511, 0.741455966636511, 0.741455966636511,
0.741455966636511, 0.741455966636511, 0.741455966636511, 0.836255759358446,
0.836255759358446, 0.836255759358446, 0.836255759358446, 0.972845769144406,
0.972845769144406, 0.668098499995622, 0.668098499995622, 0.668098499995622,
0.675298126509343, 0.675298126509343, 0.675298126509343, 0.67529812650932
), M4 = c(0.984893346832548, 0.984893346832548, 0.984893346832548,
0.898279272093836, 0.898279272093836, 0.552069187025303, 0.552069187025303,
0.552069187025303, 0.552069187025303, 0.552069187025303, 0.305500984879433,
0.305500984879433, 0.128186286770186, 0.208744159967084, 0.354671346857167,
0.270933172866875, 0.270933172866875, 0.270933172866875, 0.270933172866875,
0.270933172866875, 0.270933172866875, 0.270933172866875, 0.270933172866875,
0.270933172866875, 0.270933172866875, 0.157445543929907, 0.157445543929907,
0.157445543929907, 0.157445543929907, 0, 0, 1, 1, 1, 0.984893346832548,
0.984893346832548, 0.984893346832548, 0.984893346832602)), row.names = c(NA,
38L), class = "data.frame")
df2<-c(M1 = 0.269895498627542, M2 = 0.134785508607432, M3 = 0.147347188053672,
M4 = 0.447971804711354)
CodePudding user response:
A possible base R solution:
t(apply(df1, 1, \(x) x*df2))