R中两个向量的乘置换 [英] Multiply permutations of two vectors in R

查看:109
本文介绍了R中两个向量的乘置换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个长度为4的向量,并且想要向量的排列的乘积:

I've got two vectors of the length 4 and want a multiplication of the permutations of the vector:

A=(a1,a2,a3,a4)
B=(b1,b2,b3,b4)

我想要:

a1*b1;a1*b2;a1*b3...a4*b4

作为具有已知顺序的列表或具有row.names = A和colnames = B的data.frame

as a list with known order or data.frame with row.names=A and colnames=B

推荐答案

使用outer(A,B,'*')将返回矩阵

x<-c(1:4)
y<-c(10:14)
outer(x,y,'*')

返回

     [,1] [,2] [,3] [,4] [,5]
[1,]   10   11   12   13   14
[2,]   20   22   24   26   28
[3,]   30   33   36   39   42
[4,]   40   44   48   52   56

,如果您希望将结果显示在列表中,则可以进行

and if you want the result in a list you then can do

z<-outer(x,y,'*')
z.list<-as.list(t(z))

head(z.list)返回

[[1]]
[1] 10

[[2]]
[1] 11

[[3]]
[1] 12

[[4]]
[1] 13

[[5]]
[1] 14

[[6]]
[1] 20

x1 * y1,x1 * y2,x1 * y3,x1 * y4,x2 * y1,... >)

which is x1*y1, x1*y2, x1* y3, x1*y4, x2*y1 ,... (if you want x1*y1, x2*y1, ... replace t(z) by z)

这篇关于R中两个向量的乘置换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆