根据其他列表的值对一个列表的值进行排序

根据其他列表的值对一个列表的值进行排序

本文介绍了对R中的列表列表进行排序:根据其他列表的值对一个列表的值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关列表列表排序的帮助.假设我列出了以下孩子:

I need some help with sorting a list of lists. Suppose I have a list of children given as following:

a <- list(name = "Ann", age = 9)
b <- list(name = "Bobby", age = 17)
c <- list(name = "Alex", age = 6)

my.list <- list(a, b, c)

我想按年龄对他们的名字进行排序,因此请获得以下信息:

I would like to sort their names by their age, so receive the following:

> "Alex" "Ann" "Bobby"

推荐答案

a <- list(name = "Ann", age = 9)
b <- list(name = "Bobby", age = 17)
c <- list(name = "Alex", age = 6)

L <- list(a,b,c)
ages <- sapply(L,"[[","age")
names <- sapply(L,"[[","name")
names[order(ages)]

这篇关于对R中的列表列表进行排序:根据其他列表的值对一个列表的值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:42