R成对积

扫码查看
本文介绍了R成对积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到一个向量的成对乘积,比如说

I'm trying to get the pairwise products of a vector, say

a = c(1,2,3,4)

我想要的是

2,3,4,6,8,12(按此顺序).

2,3,4,6,8,12 (in that order).

我试过使用外部:

outer(1:4,2:4)

这给了我一个矩阵,其中包含我想要的产品,但我不确定如何以缩放到更高维度向量的方式从矩阵中提取它们.

and that gives me a matrix that includes the products I want but I'm not sure how to extract them from the matrix in a way that scales to vectors of higher dimensions.

谢谢!

推荐答案

combn() 非常适合这种事情:

a <- 1:4

combn(a, m = 2, FUN = prod)
# [1]  2  3  4  6  8 12

这篇关于R成对积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:46
查看更多