R:根据元素中的值对列表进行排序,理想情况是链式处理与purrr / tidyverse一起使用

问题描述 投票:0回答:1

我想根据条件在列表中排列项目,就像dplyr :: arrange在data.frames中所做的(同时保持链接的语法)。

    ## Something along the lines of:

    list(
        list( foo="baz"  ),
        list( foo="test" ),
        list( foo="bar"  )
    ) %>% arrange( foo )

    ## .. would result in a structure equivalent of:
    list(
        list( foo="bar"  ),
        list( foo="baz"  ),
        list( foo="test" )
    )

我希望存在类似的东西,但找不到它

编辑:为了清楚起见,我删除了变量“ l”

EDIT2:不能使用dplyr :: arrange更改路径,因为元素可能不支持它,或者它们可能没有匹配的名称

r sorting tidyverse purrr
1个回答
1
投票

您可以unlistsort,并使用relist将其放回相同的结构。

relist(sort(unlist(l)), l)

#[[1]]
#[[1]]$foo
#[1] "bar"


#[[2]]
#[[2]]$foo
#[1] "baz"


#[[3]]
#[[3]]$foo
#[1] "test"
© www.soinside.com 2019 - 2024. All rights reserved.