简体   繁体   English

创建矩阵列的按元素乘积

[英]Create element-wise product of matrix columns

x1  x2  x3  x11 x12 x13 x22 x23 x33
1   5   9   1   5   9   25  45  81
2   6   10  4   12  20  36  60  100
3   7   11  9   21  33  49  77  121
4   8   12  16  32  48  64  96  144

When x1 , x2 , and x3 are given, I would like to create a matrix or data frame x11, x12, x13, x22, x23 , and x33 which are element-wise product of vectors x1, x2 , and x3 . 给定x1x2x3 ,我想创建一个矩阵或数据帧x11, x12, x13, x22, x23x33 ,它们是向量x1, x2x3 x11, x12, x13, x22, x23元素乘积。

Actually I would like to to this for more vectors (eg x1 ~ x6 ) to high order (3rd or 4th). 实际上,我希望更多的矢量(例如x1 ~ x6 )达到高阶(第3或第4)。 Is there an R command which can do this? 有可以执行此操作的R命令吗?

We can do the combination with expand.grid to find all the combination of columns 1:3, then loop through the rows, subset the dataset and get the * of those 我们可以使用expand.grid进行组合,以找到列1:3的所有组合,然后遍历各行,对数据集进行子集化,并获得其中的*

nm1 <- names(df1)[1:3]
apply(expand.grid(nm1, nm1), 1, FUN = function(x) Reduce(`*`, df1[x]))

The above output gives all the combinations, but suppose if we want to remove the combinations that are mirror image 上面的输出给出了所有组合,但是假设我们要删除镜像的组合

#expand the names to two columns with each combination
d1 <- expand.grid(nm1, nm1)
#remove the rows that are duplicates
d2 <- d1[!duplicated(t(apply(d1, 1, sort))),]
#apply the function and change the column names
d3 <- apply(d2, 1, FUN = function(x) Reduce(`*`, df1[x]))
colnames(d3) <- do.call(paste0, d2)

and if needed cbind with the first 3 columns 并在需要cbindcbindcbind

cbind(df1[1:3], d3)

Another option is combn 另一个选择是combn

d1 <- as.data.frame(combn(nm1, 2, FUN = function(x) Reduce(`*`, df1[x])))
nm2 <- combn(nm1, 2, FUN = paste, collapse="")
names(d1) <- nm2
d2 <- setNames(as.data.frame(df1[1:3]^2), paste0(nm1, nm1))
cbind(df1[1:3], d1, d2)

You could also do this quite vectorized in base R: 您也可以在基数R中将其向量化:

# Data
m <- matrix(1:12,4,3)

cl <- ncol(m)
res <- cbind(m, m[,rep(seq(cl), cl:1)] * m[,unlist(Map(":", 1:cl, rep(cl,cl)))])

      # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,]    1    5    9    1    5    9   25   45   81
# [2,]    2    6   10    4   12   20   36   60  100
# [3,]    3    7   11    9   21   33   49   77  121
# [4,]    4    8   12   16   32   48   64   96  144

Basically you first select the columns for which you want to have the product and then do them all at once. 基本上,您首先要选择要拥有其产品的列,然后立即全部完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM