Skip to content
Prev 294967 / 398506 Next

Sort across multiple csv

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