【问题标题】:Creating interaction terms from two matrices从两个矩阵创建交互项
【发布时间】:2018-04-10 16:38:22
【问题描述】:

假设我有两个矩阵:

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 就是w1x1 的乘积

我该怎么做?

【问题讨论】:

    标签: r matrix


    【解决方案1】:

    基础 R 解决方案:

    res <- apply(w, 2, function(i) { 
        i * x
        })
    
    wx <- do.call(cbind, res)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多