[R] Sort across multiple csv

MacQueen, Don macqueen1 at llnl.gov
Fri May 18 19:57:42 CEST 2012


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


-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 5/18/12 9:56 AM, "Matthew Ouellette" <mouellette89 at gmail.com> wrote:

>Dear R help list,
>
>I am very new to R and I apologize in advance if this has been answered
>before.  I have done my best to google/R search what I need but no luck.
> Here is what I am attempting:
>
>I have hundreds of .csv files that I need to sort based on a single column
>of alphanumeric data.  All of the files contain matrices that have
>identical dimensions and headers, however the data table doesn't begin
>until the 74th line in each file.  Doing some searching, I have been able
>to create an object with elements consisting of each file in the folder
>containing the targets (please note this is my working directory):
>
>filenames<-list.files()
>alldata<-lapply(filenames, read.csv, skip=73, header=TRUE)
>
>At this point I believe I have created an object with N elements (where
>N=#
>files in the wd), each containing the matrix I am attempting to sort.  I
>am
>completely lost as to how I can sort each matrix based on a single column
>(say, "Name") and then either overwrite the source files or write to a new
>directory all of the sorted data.  I half wonder if I should be creating
>individual objects for each file that I read in, but I haven't been able
>to
>figure this out either.  Please note that I am trying to sort these files
>individually - would a loop be more efficient?
>
>I appreciate the help,
>BustedAvi
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list