调整列表元素的顺序

huangapple go评论53阅读模式
英文:

Reordering elements of a list

问题

我正在尝试在R中创建一个函数,该函数以列表作为输入,并根据每个元素的平均值对其进行重新排序(例如,第一个元素将是具有最大平均值的元素,依此类推)。我试图找到一种无需使用循环的最简单方法来实现它。我尝试对平均值进行排序,但无法弄清楚如何让元素与平均值关联并一起移动。是否有任何建议或建议将不胜感激。

function1 <- function(x){
  return(sort(x, decreasing = TRUE, index.return = TRUE)$ix)
}
function2 <- function(x) {
  return(x[function1(sapply(x, mean))])
}

testlist <- list(c(10, 5, 1), c(3, 2), c(77, 90, 1), c(23, 34), c(2, 35, 22))
function2(testlist)

这是经过修正的代码,它将根据每个元素的平均值对列表进行重新排序。

英文:

I'm trying to create a function in R that takes a list as an input and reorder its elements based on the mean of each element (for example, the first element will be the one with the largest mean value, etc.) I'm trying to find the easiest way to do it without having to use any loops. I tried to sort the mean like below but couldn't figure out how to have the elements associated with the mean to move along. Any suggestions or advice would be appreciated.

function1 &lt;- function(x){
  return(sort(mean(x), decreasing = T))
}
function2 &lt;- function(x) {
  return(lapply(function1, x))
}

testlist &lt;- list(c(10, 5, 1), c(3, 2), c(77, 90, 1), c(23, 34), c(2, 35, 22))
function2(testlist)

答案1

得分: 2

这里是一个方法,实际上就是 testlist[order(sapply(testiest, mean))],但包装成了一个函数。这个想法是,sapply() 返回一个向量,其中包含了每个列表元素的均值,order() 返回均值顺序的元素编号。然后,你将根据均值给出的排序后的元素编号用于索引列表的值。

testlist <- list(c(10, 5, 1), c(3, 2), c(77, 90, 1), c(23, 34), c(2, 35, 22))

function1 <- function(x){
  x[order(sapply(x, mean))]
}

function1(testlist)
#> [[1]]
#> [1] 3 2
#> 
#> [[2]]
#> [1] 10  5  1
#> 
#> [[3]]
#> [1]  2 35 22
#> 
#> [[4]]
#> [1] 23 34
#> 
#> [[5]]
#> [1] 77 90  1

创建于 2023-02-18,使用 reprex package (v2.0.1)。

英文:

Here's one way that is really just testlist[order(sapply(testiest, mean))], but put inside a function. The idea is that sapply() returns a vector that gives the mean of each list element, order() gives the element numbers in order of the mean. Then, you are giving that ordered element numbers based on the mean to index the values of the list.

testlist &lt;- list(c(10, 5, 1), c(3, 2), c(77, 90, 1), c(23, 34), c(2, 35, 22))

function1 &lt;- function(x){
  x[order(sapply(x, mean))]
}

function1(testlist)
#&gt; [[1]]
#&gt; [1] 3 2
#&gt; 
#&gt; [[2]]
#&gt; [1] 10  5  1
#&gt; 
#&gt; [[3]]
#&gt; [1]  2 35 22
#&gt; 
#&gt; [[4]]
#&gt; [1] 23 34
#&gt; 
#&gt; [[5]]
#&gt; [1] 77 90  1

<sup>Created on 2023-02-18 by the reprex package (v2.0.1)</sup>

答案2

得分: 2

可以使用sapply函数,然后按照均值降序排列元素,使具有最高均值的第一个元素:

```R
function2 <- function(z) {
  order_ind <- order(sapply(z, mean), decreasing = TRUE)
  z[order_ind]
}

sapply()计算每个元素的均值,
z[order_ind]使用排序后的索引输出有序列表。

function2(testlist)

结果:

[[1]]
[1] 77 90  1

[[2]]
[1] 23 34

[[3]]
[1]  2 35 22

[[4]]
[1] 10  5  1

[[5]]
[1] 3 2
英文:

You can use sapply function and then order the elements in decreasing ordedr with the first element with the highest mean :

function2 &lt;- function(z) {
  order_ind &lt;- order(sapply(z, mean), decreasing = TRUE)
  z[order_ind]
}

sapply() computes the mean value of each element and
z[order_ind] outputs the ordered list using the ordered indexes

function2(testlist)

the results:

[[1]]
[1] 77 90  1

[[2]]
[1] 23 34

[[3]]
[1]  2 35 22

[[4]]
[1] 10  5  1

[[5]]
[1] 3 2

答案3

得分: 0

使用collapse中的fmean

library(collapse)
testlist[order(fmean(testlist))]

输出

[[1]]
[1] 3 2

[[2]]
[1] 10  5  1

[[3]]
[1]  2 35 22

[[4]]
[1] 23 34

[[5]]
[1] 77 90  1

数据

testlist <- list(c(10, 5, 1), c(3, 2), c(77, 90, 1), c(23, 34), c(2, 35, 22))

这是给定代码的翻译部分,不包含问题或其他内容。

英文:

Using fmean from collapse

library(collapse)
testlist[order(fmean(testlist))]

-output

[[1]]
[1] 3 2

[[2]]
[1] 10  5  1

[[3]]
[1]  2 35 22

[[4]]
[1] 23 34

[[5]]
[1] 77 90  1

data

testlist &lt;- list(c(10, 5, 1), c(3, 2), c(77, 90, 1), c(23, 34), c(2, 35, 
22))

huangapple
  • 本文由 发表于 2023年2月19日 08:17:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定