How to sort all dataframes in a list of dataframes on the same column?

后端 未结 3 1480
后悔当初
后悔当初 2020-12-06 13:25

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 li

相关标签:
3条回答
  • 2020-12-06 13:51

    lapply is your friend ;)

    You could do it using arrange from dplyr pacakge like this:

    library(dplyr)
    newlist <- lapply(dataframeslist, function(df){arrange(df,enrichment)})
    

    or without dplyr like this:

    newlist <- lapply(dataframeslist, function(df){df[order(df$enrichment),]})
    
    0 讨论(0)
  • 2020-12-06 14:07

    Use lapply:

    sorted_dataframes_list <- lapply(dataframes_list, function(df){
                                     df[order(df$enrichment),]
                                     })
    
    0 讨论(0)
  • 2020-12-06 14:10

    This is a data.table solution.

    require(data.table)
    
    # Convert to data.table
    dataframes_list <- lapply(dataframes_list, data.table)
    
    # Setting a key sorts a data.table
    lapply(dataframes_list, setkey, enrichment)
    
    0 讨论(0)
提交回复
热议问题