如何对同一列的数据帧列表中的所有数据帧进行排序? [英] How to sort all dataframes in a list of dataframes on the same column?

查看:81
本文介绍了如何对同一列的数据帧列表中的所有数据帧进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧dataframes_list的列表.例如,我将dput(dataframes_list)放在底部.我想对列enrichment上的列表中的所有数据框进行排序.我可以使用

I have a list of dataframes dataframes_list. For an example, I put the dput(dataframes_list) at the bottom. I want to sort all dataframes in the list on the column enrichment. I can sort one dataframe with

first_dataframe <- dataframes_list[[1]]
sorted_dataframe <- first_dataframe[order(first_dataframe$enrichment),] 

如何为dataframes_list中的每个数据帧执行此操作?

How can I do this for every dataframe in dataframes_list?

示例dataframes_list:

list(structure(list(rank = c(1, 2, 3, 4, 5, 6), cmap.name = c("meclocycline", 
"pimozide", "isocorydine", "alvespimycin", "15-delta prostaglandin J2", 
"naloxone"), mean = c(-0.471, 0.504, -0.49, 0.193, 0.296, -0.383
), n = c(4, 4, 4, 12, 15, 6), enrichment = c(-0.869, 0.855, -0.859, 
0.539, 0.476, -0.694), p = c("0.00058", "0.00058", "0.00068", 
"0.00072", "0.00122", "0.00199"), specificity = c("0", "0.0302", 
"0", "0.069", "0.2961", "0.0155"), percent.non.null = c(100, 
100, 100, 50, 60, 83), signature_name = c("day1_day3__sham3_strict", 
"day1_day3__sham3_strict", "day1_day3__sham3_strict", "day1_day3__sham3_strict", 
"day1_day3__sham3_strict", "day1_day3__sham3_strict")), .Names = c("rank", 
"cmap.name", "mean", "n", "enrichment", "p", "specificity", "percent.non.null", 
"signature_name"), row.names = c(NA, 6L), class = "data.frame"), 
    structure(list(rank = c(1, 2, 3, 4, 5, 6), cmap.name = c("trichostatin A", 
    "daunorubicin", "ketotifen", "quipazine", "minoxidil", "ricinine"
    ), mean = c(-0.319, -0.515, 0.486, 0.476, 0.316, -0.481), 
        n = c(182, 4, 4, 4, 5, 4), enrichment = c(-0.384, -0.883, 
        0.862, 0.861, 0.812, -0.816), p = c("0", "0.00046", "0.0005", 
        "0.0005", "0.00058", "0.00209"), specificity = c("0.3762", 
        "0.0175", "0.0053", "0.0065", "0", "0.006"), percent.non.null = c(56, 
        100, 100, 100, 60, 100), signature_name = c("hour4_day1_day3__sham3_strict", 
        "hour4_day1_day3__sham3_strict", "hour4_day1_day3__sham3_strict", 
        "hour4_day1_day3__sham3_strict", "hour4_day1_day3__sham3_strict", 
        "hour4_day1_day3__sham3_strict")), .Names = c("rank", 
    "cmap.name", "mean", "n", "enrichment", "p", "specificity", 
    "percent.non.null", "signature_name"), row.names = c(NA, 
    6L), class = "data.frame"))

推荐答案

使用lapply:

sorted_dataframes_list <- lapply(dataframes_list, function(df){
                                 df[order(df$enrichment),]
                                 })

这篇关于如何对同一列的数据帧列表中的所有数据帧进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆