Skip to content

Sort across multiple csv

4 messages · Matthew Ouellette, MacQueen, Don, Rui Barradas +1 more

#
You appear to have a good start.

If you type
  alldata[[1]]
do you get what you expect for the first file?

This is not tested, but I would start with something like this:

sorteddata <- lapply(alldata, function(df) df[order(df$Name),] )

## then this will overwrite
for (id in seq(filenames)) {
  write.csv( sorteddata[[id]] , filenames[id] )
}

## or changed something like this for new files
  write.csv( sorteddata[[id]] , paste('sorted_',filenames[id],sep='') )

An you'll want to check the other arguments to write.csv(), or possibly
use write.table().

For learning purposes:

tmp <- alldata[[1]]
tmp[order(tmp$Name),]  ## to sort by Name
tmp[order(tmp[,2]),]   ## to sort by 2nd column
#
Hello,

Try the following.


# Make some data
alldata <- list(matrix(rnorm(12), ncol=3), matrix(sample(100), ncol=10))
(alldata <- lapply(alldata, function(x){colnames(x) <- c("Name",
LETTERS[2:ncol(x)]); x}))

# This does the trick
all.order <- lapply(alldata, function(x) order(x[, "Name"]))
lapply(seq.int(length(alldata)), function(i) alldata[[i]][all.order[[i]], ])


Hope this helps,

Rui Barradas

BustedAvi wrote
--
View this message in context: http://r.789695.n4.nabble.com/Sort-across-multiple-csv-tp4630531p4630537.html
Sent from the R help mailing list archive at Nabble.com.
#
On May 18, 2012, at 12:56 PM, Matthew Ouellette wrote:

            
You should learn to use precise terminology to refer to R objects. You  
have a list of dataframes (not matrices)

You can loop over then and return a list of transformed (.e.g. sorted)  
dataframes:

alldata <- lapply (alldata, function(x) x[order(x[["Name"]], ] )
The above code would overwrite.
If you didn't want it overwritten then assign it to a different name.
Much better to stick with lists.
`lapply` is really a loop.
David Winsemius, MD
West Hartford, CT