Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据R中的内部列表变量对列表进行排序_R_List_Sorting_Vector - Fatal编程技术网

根据R中的内部列表变量对列表进行排序

根据R中的内部列表变量对列表进行排序,r,list,sorting,vector,R,List,Sorting,Vector,我已经创建了一个列表列表,比如说parentList使用R中的向量。parentList包含100个列表childList1、childList2等等。每个这样的子列表都包含一个元素列表,包括孙子变量1、孙子变量2等等。除父列表外,所有列表和变量均未命名 我想根据每个childList的第二个元素variable2对parentList进行排序。我可以使用parentList[[2]][2]获取此变量的值。但我不太确定如何对整个列表进行排序 我目前正试图将其分类如下: 我认为这应该行得通。首先提

我已经创建了一个列表列表,比如说parentList使用R中的向量。parentList包含100个列表childList1、childList2等等。每个这样的子列表都包含一个元素列表,包括孙子变量1、孙子变量2等等。除父列表外,所有列表和变量均未命名

我想根据每个childList的第二个元素variable2对parentList进行排序。我可以使用parentList[[2]][2]获取此变量的值。但我不太确定如何对整个列表进行排序

我目前正试图将其分类如下:
我认为这应该行得通。首先提取值,然后使用它们对父列表进行排序更容易一些

childList1 <- list(grandChildVariable1 = 1,
               grandChildVariable2 = 10)
childList2 <- list(grandChildVariable1 = 1,
               grandChildVariable2 = 30)
childList3 <- list(grandChildVariable1 = 1,
               grandChildVariable2 = 20)
parentList <- list(childList1, childList2,childList3)

x <- sapply(parentList, function(x) x[[2]])

orderedParentList <- parentList[order(x)]
str(orderedParentList)

我能够找出“orderVector1”问题中未实现类型“list”的根本原因。原因是一些childList元素为NULL。当我验证parentList不包含空值时,我能够正确地完成排序


使用排序列表,这可能更整洁:x而不是循环编辑您的答案,使其更容易访问。我也认为这是一个好的。