我们说我有两个矩阵:
w <- data.frame(w1 = rnorm(10), w2 = rnorm(10), w3 = rnorm(10), w4 = rnorm(10))
x <- data.frame(x1 = rnorm(10), x2 = rnorm(10), x3 = rnorm(10), x4 = rnorm(10))
我想生成一个矩阵wx,它有列
w1x1, w1x2, w1x3, w1x4, w2x1, w2x2, ... , w4x4
每列只是两列的产物,每列中有一列。例如,w1x1
只是w1
和x1
我该怎么做?
答案 0 :(得分:3)
Base R解决方案:
res <- apply(w, 2, function(i) {
i * x
})
wx <- do.call(cbind, res)