[R] Sort across multiple csv

Rui Barradas ruipbarradas at sapo.pt
Fri May 18 20:09:22 CEST 2012


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
> 
> 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@ 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.
> 


--
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.



More information about the R-help mailing list