Product between all combinations of a vector's elements

前端 未结 3 1914
囚心锁ツ
囚心锁ツ 2021-01-19 16:27

Suppose I have a vector c(1, 2, 3, 4) with no duplicated values. I need a vector c(1 * 2, 1 * 3, 1 * 4, 2 * 3, 2 * 4, 3 * 4), so the multiplication

3条回答
  •  清歌不尽
    2021-01-19 16:56

    We can use combn with anonymous function call

    combn(vec, 2, FUN = function(x) x[1] * x[2])
    #[1]  2  3  4  6  8 12
    

    data

    vec <- 1:4
    

提交回复
热议问题