gpt4 book ai didi

r - 与列表中所有向量的成对比较

转载 作者:行者123 更新时间:2023-12-02 16:04:53 24 4
gpt4 key购买 nike

例如,我有一个包含 5 个向量的列表,并且想要比较它们中的每一个。示例

L = list(c(1:5), c(-1:-5), c(3:7), c(-4:-8), c(5:9))
[[1]]
[1] 1 2 3 4 5

[[2]]
[1] -1 -2 -3 -4 -5

[[3]]
[1] 3 4 5 6 7

[[4]]
[1] -4 -5 -6 -7 -8

[[5]]
[1] 5 6 7 8 9

我还有一个函数可以在列表的 5 个元素之间进行成对比较:

foo = function(x, y) t(x) %*% abs(y)

我想应用 foo 对我的列表进行成对比较。例如:成对比较(foo 的“x”和“y”):

[[1]]and [[2]]
[[1]]and [[3]]
[[1]]and [[4]]
[[1]]and [[5]]
[[2]]and [[3]]
[[2]]and [[4]]
[[2]]and [[5]]
[[3]]and [[4]]
[[3]]and [[5]]
[[4]]and [[5]]
...
My original file has 1000 elements

我尝试使用:拉普利(L,foo)但我有以下消息错误:```参数“y”丢失,没有默认值````

如何将 foo 应用到我的列表?

最佳答案

也许是 combnapplylapply 的组合?

L = list(c(1:5), c(-1:-5), c(3:7), c(-4:-8), c(5:9))
foo = function(x, y) t(x) %*% abs(y)

setNames(lapply(apply(combn(seq_along(L), 2), 2, function(x) L[x]),
function(z) foo(z[[1]], z[[2]])),
apply(combn(seq_along(L), 2), 2, paste, collapse = "-"))
#> $`1-2`
#> [,1]
#> [1,] 55
#>
#> $`1-3`
#> [,1]
#> [1,] 85
#>
#> $`1-4`
#> [,1]
#> [1,] 100
#>
#> $`1-5`
#> [,1]
#> [1,] 115
#>
#> $`2-3`
#> [,1]
#> [1,] -85
#>
#> $`2-4`
#> [,1]
#> [1,] -100
#>
#> $`2-5`
#> [,1]
#> [1,] -115
#>
#> $`3-4`
#> [,1]
#> [1,] 160
#>
#> $`3-5`
#> [,1]
#> [1,] 185
#>
#> $`4-5`
#> [,1]
#> [1,] -220

reprex package 于 2021 年 10 月 25 日创建(v2.0.0)

关于r - 与列表中所有向量的成对比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69712675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com