Andrew Brown
Andrew Brown

Reputation: 333

How to sort multiple dataframes in R

I have 100 data dataframes in R and would like to sort each of them by a column prior to using rbind to combine the dataframes.

I can use data <- data[order(data$V2),] for a small number of dataframes but I am unable to automate the process for hundreds of dataframes using loops (or another helpful construct).

I intend to use x <- grep("data\\d",ls(),perl=TRUE,value=TRUE) and do.call(rbind, lapply(x, get)) to combine the sorted dataframes.

Upvotes: 3

Views: 732

Answers (1)

akrun
akrun

Reputation: 887241

We can use the pattern argument in ls to return the object names, wrap it with mget and it will get the values of the objects in a list.

 lst <- mget(ls(pattern='data\\d'))

We loop through the list elements with lapply and order using the 'V2' column (assuming that the column exist in all the datasets)

 lst1 <- lapply(lst, function(x) x[order(x$V2),])

If we want to change the original data object (not recommended as most of the operations can be done within the list environment), we can use list2env

list2env(lst1, envir=.GlobalEnv)

Or we can rbind all the datasets

df1 <- do.call(rbind, lst1)

It may be better to read all the datasets in a list directly rather than creating individual objects. Suppose, if we want to read all the files in the working directory,

files <- list.files()
lst <- lapply(files, read.csv, stringsAsFactors=FALSE)

Upvotes: 4

Related Questions