I have two data.frames
: df
and weights
(code below).
df
looks like this:
id a b d EE f
1 this 0.23421153 -0.02324956 0.5457353 0.73068586 0.5642554
2 is 0.28378641 0.36346241 1.0190496 1.97715019 -1.190179
3 an -0.04372133 0.07412557 -0.4510299 1.8442713 -0.5301328
4 example -0.18139887 0.10404329 -1.0871962 1.46920108 0.4685703
5 data.frame 0.24235498 -0.1501064 -1.1183967 0.22884407 0.4272259
6 for -0.72718239 0.16337997 1.2635683 0.44206945 0.7303647
7 stackoverflow 0.25203942 -0.1772715 -0.3371532 -0.29167792 -0.7065494
8 please -0.11047364 -0.06631552 0.4342659 -1.49584522 0.2837016
9 help -0.1136639 0.22414253 0.4284864 1.59096047 0.2915938
10 me -0.3677288 0.05974474 -0.1136177 0.02322094 -0.6533994
How can I multiply the values in each of the columns by the corresponding weights in the weights
data.frame
?
Expected result:
id a b d EE f
1 this 0.749476896 -0.1743717 5.29363241 NA 4.17548996
2 is 0.908116512 2.725968075 9.88478112 NA -8.8073246
3 an -0.139908256 0.555941775 -4.37499003 NA -3.92298272
4 example -0.580476384 0.780324675 -10.54580314 NA 3.46742022
5 data.frame 0.775535936 -1.125798 -10.84844799 NA 3.16147166
6 for -2.326983648 1.225349775 12.25661251 NA 5.40469878
7 stackoverflow 0.806526144 -1.32953625 -3.27038604 NA -5.22846556
8 please -0.353515648 -0.4973664 4.21237923 NA 2.09939184
9 help -0.36372448 1.681068975 4.15631808 NA 2.15779412
10 me -1.17673216 0.44808555 -1.10209169 NA -4.83515556
Code:
set.seed(12345)
df <- data.frame(id=c("this", "is", "an", "example", "data.frame", "for",
"stackoverflow", "please", "help", "me"), a=rnorm(10,0,0.4), b=rnorm(10,0,0.2),
d=rnorm(10,0,0.7), EE=rnorm(10,0,0.9), f=rnorm(10,0,0.5))
weights <- data.frame(V1=as.numeric(c("3.2", "7.5", "2.2", "9.7", "5.4", "7.4", "2.1",
"5.0", "3.3", "7.6", "3.6", "7.7", "7.1", "3.3", "9.8", "9.2", "2.5", "6.2", "4.1", "8.7",
"3.3", "9.3", "8.3")))
rownames(weights) <- paste(letters[1:23])
across
. And I was sure that it is possible. Thank you for this solution akrun! – Clueless