[R] Adding more files to list

Sebastian Gibb lists at sebastiangibb.de
Wed Apr 25 13:53:45 CEST 2012


Hi Brian,

On Wednesday 25 April 2012 13:15:03 Brian Flatley wrote:
> I have a main directory  - "Spectra"
> 
> In this folder I have two subfolders "Normal"  and "Case"
The following should be enough:

library("readBrukerFlexData")
Spectra <- mqReadBrukerFlex("Spectra/");

> ...
> The following command will open and list all my files from the Normal
> subfolder (70 files containing spectrums)
>
> Spectra <- mqReadBrukerFlex(file.path("Spectra/", "Normal"));
> 
> How do I add the "Case" folder to the list (another 70 spectra), so that I
> can then process all the files in the same manner and yet maintain their
> origin so when I want to do stats analyse on them I can differentiate??
> 
To combine a list you could use "c":

SpectraNormal <- mqReadBrukerFlex(file.path("Spectra/", "Normal"));
SpectraCase <- mqReadBrukerFlex(file.path("Spectra/", "Case"));

Spectra <- c(SpectraNormal, SpectraCase);

To maintain the origin you could store the indices:

normal <- 1:length(SpectraNormal);
cases <- (normal+1):(normal+1+length(SpectraCase));

But I would prefer to fetch this information from the MassSpectrum metaData 
slot:

filenames <- sapply(Spectra, function(x) {return(metaData(x)$file); });
normal <- grepl(pattern="/Normal/", x=filenames);
classes <- as.factor(ifelse(normal, "Normal", "Case"));

Now you could access the different cases in the following way:
Spectra[classes=="Normal"]
Spectra[classes=="Case"]

Bye,

Sebastian



More information about the R-help mailing list