I have two data frames that have common columns.
# Generate DF1
set.seed(219)
x0 <- rnorm(5, 22, 17)
x2 <- rnorm(5, 44, 15)
x3 <- rnorm(5, 56, 13)
x7 <- rnorm(5, 0, 3)
x9 <- rnorm(5, 28, 31)
x10 <- rnorm(5, 4, 75)
x11 <- rnorm(5, 7, 1)
dat1 <- data.frame(x0,x2,x3,x7,x9, x10, x11)
dat1$ID1 <- rownames(dat1)
# Generate DF2
x1 <- rnorm(10, 2, 19)
x2 <- rnorm(10, 4, 18)
x3 <- rnorm(10, 5, 17)
x4 <- rnorm(10, 7, 16)
x5 <- rnorm(10, 8, 51)
x6 <- rnorm(10, 9, 5)
x7 <- rnorm(10, 0, 3)
x8 <- rnorm(10, 34, 2)
x9 <- rnorm(10, 28, 1)
dat2 <- data.frame(x1,x2,x3,x4,x5,x6,x7,x8,x9)
dat2$ID2 <- rownames(dat2)
Note that DF1 has 5 rows while DF2 has 10 rows. Also, similar columns names in each data frame does not mean that both columns are the same in value.
This is what I would like to do:
Since
DF1has 5 rows, I need to create 5 columns inDF2and let's call themy1, y2, y3, y4, y5.Here is how to compute
y1: I need to take the first row inDF1and multiply it with similar columns inDF2for all the rows. The size ofy1will be (10 rows and 1 column). And I need to calculate the following for each row in DF2.y1 = x0 + x2(DF1)*x2(DF2) + x3(DF1)*x3(DF2) + x7(DF1)*x7(DF2) + x9(DF1)*x9(DF2)
Similarly, For y2, we need to start in the second row of DF1 ... Etc.
In terms of vectors and matrices, here is how to compute y1.
Let the first row in DF1 as (x01, x21, x31, x71, x91, x101, x111, ID11). Then first first value of y1 (remember y1 is 10*1):
y11 = x01 + x21(DF1)*x21(DF2) + x31(DF1)*x31(DF2) + x71(DF1)*x71(DF2) + x91(DF1)*x91(DF2).
Second value of y1:
y12 = x01 + x21(DF1)*x22(DF2) + x31(DF1)*x32(DF2) + x71(DF1)*x72(DF2) + x91(DF1)*x92(DF2).
...
finaly, the 10th value of y1 is:
y110 = x01 + x21(DF1)*x210(DF2) + x31(DF1)*x310(DF2) + x71(DF1)*x710(DF2) + x91(DF1)*x910(DF2).
How can I implement my algorithm?
x0be constant 'y2', 'y3', etc.. i.e. whether the formula differs for 'y2', 'y3', etc - akrun