I have the following two example data.frames.
set.seed(12345)
df1 = data.frame(a=c(rep("a",8), rep("b",5), rep("c",7), rep("d",10)),
b=rnorm(30, 6, 2),
c=rnorm(30, 12, 3.5),
d=rnorm(30, 8, 3)
)
df2 = data.frame(p=c("b", "c", "d"),
q=c(1.43, 2.14, -2.03)
)
I would like to create a new data.frame using the base data in df1 and a weighted average using the multiplication factors in df2. The new output df3 would be the same as df1 but with a new column added with values: the row average of ("b" multiplied by 1.43 + "c" multiplied by 2.14, "d" multiplied by -2.03), so that the result is df3:
df3 = data.frame(a=c(rep("a",8), rep("b",5), rep("c",7), rep("d",10)),
b=rnorm(30, 6, 2),
c=rnorm(30, 12, 3.5),
d=rnorm(30, 8, 3),
new=c("24.8645275","44.67937096","29.68621196","19.26714231",
"25.23142628","27.65882406","11.98590475","-4.92298683",
"27.29998443","23.47463009","25.80746763","10.16714534",
"17.52916576","12.33418399","13.73084634","25.55675733",
"-0.13100614","26.26381852","22.69296138","2.86696252",
"12.27184531","30.41901753","18.43221894","1.12637556",
"2.51020245","13.89381723","17.7266222","27.83995036",
"32.569782","-5.04627832")
)
How could I do this please?