Essentially, x can have any number of rows and columns, while y can only have any number of columns. When the two dataframes have the same column names, I would like them to be multiplied. If the columns in x do not exist in y, then they can be left alone (or multiplied by 1).
How is this different from before ? can you emphasise the difference ?
your screenshot for y shows var3 as 5 but your code has var 2 as 5. I went with the screenshot.
I provide the same solution you were provided before, but put into a function, so hopefully you can reuse it with greater ease.
x = data.frame(var1 = c(1, 2, 3),
var2 = c(3, 4, 5),
var3 = c(5, 6, 7),
var4 = c(7, 8, 9))
y = data.frame(var1 = 2,
var3 = 5)
mycalc <- function(x,y){
require(dplyr)
xm <- as.matrix(x)
#padd y with 1's
toadd <- setdiff(names(x),names(y))
y[toadd] <- 1
y <- select(y,names(x))
ym <- as.matrix(y)
xm %*% t(ym)
}
mycalc(x,y)