Hello, I have the following problem. I have a set of CSV files and they are named for a special position in a matrix (e.g. "A01.csv", "F06.csv", "H11.csv") Now, I would like to read in the data of all these files and eveluate something for each set (and write it at the position in a result matrix). How can I realize this with R? Ciao, Antje
You can use list.files to obtain the file names, see ?list.files Read the "comma seperated values" files with read.cvs, see ?read.csv JeeBee. On Tue, 24 Oct 2006 13:33:42 +0200, Antje wrote:> Hello, > > I have the following problem. I have a set of CSV files and they are > named for a special position in a matrix (e.g. "A01.csv", "F06.csv", > "H11.csv") > > Now, I would like to read in the data of all these files and eveluate > something for each set (and write it at the position in a result matrix). > How can I realize this with R? > > Ciao, > Antje > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
Try something like this (untested): # replace with your function that inputs a data frame and outputs a number F <- function(x) length(x) # replace with setwd to appropriate path setwd("/") filenames <- dir(pattern = "[.]csv$") # DFs is a list of data frames; names(DFs) will be the filenames DFs <- sapply(filenames, read.csv, simplify = FALSE) m <- matrix(0, 26, 12) rownames(m) <- LETTERS for(nam in filenames) { row <- substring(nam, 1, 1) col <- as.numeric(substring(nam, 2, 3)) m[row, col] <- F(DFs[[nam]]) } The for loop could alternately be done like this: library(gsubfn) f <- function(filename, row, col) m[row, as.numeric(col)] <<- F(DFs[[filename]]) junk <- strapply(filenames, "^(.)(..)", f) On 10/24/06, Antje <niederlein-rstat at yahoo.de> wrote:> Hello, > > I have the following problem. I have a set of CSV files and they are > named for a special position in a matrix (e.g. "A01.csv", "F06.csv", > "H11.csv") > > Now, I would like to read in the data of all these files and eveluate > something for each set (and write it at the position in a result matrix). > How can I realize this with R? > > Ciao, > Antje > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
On 10/24/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Try something like this (untested): > > # replace with your function that inputs a data frame and outputs a number > F <- function(x) length(x) > # replace with setwd to appropriate path > setwd("/") > > filenames <- dir(pattern = "[.]csv$") > # DFs is a list of data frames; names(DFs) will be the filenames > DFs <- sapply(filenames, read.csv, simplify = FALSE) > > m <- matrix(0, 26, 12) > rownames(m) <- LETTERS > > for(nam in filenames) { > row <- substring(nam, 1, 1) > col <- as.numeric(substring(nam, 2, 3)) > m[row, col] <- F(DFs[[nam]]) > } > > The for loop could alternately be done like this: > > library(gsubfn) > f <- function(filename, row, col) m[row, as.numeric(col)] <<- F(DFs[[filename]])The regular expression should be "^(.)(..).*"> junk <- strapply(filenames, "^(.)(..)", f) > > > On 10/24/06, Antje <niederlein-rstat at yahoo.de> wrote: > > Hello, > > > > I have the following problem. I have a set of CSV files and they are > > named for a special position in a matrix (e.g. "A01.csv", "F06.csv", > > "H11.csv") > > > > Now, I would like to read in the data of all these files and eveluate > > something for each set (and write it at the position in a result matrix). > > How can I realize this with R? > > > > Ciao, > > Antje > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > >
Here is a third way to do the loop: rows <- match(substring(filenames, 1, 1), LETTERS) cols <- as.numeric(substring(filenames, 2, 3)) m[cbind(rows, cols)] <- sapply(DFs, F) On 10/24/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On 10/24/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: > > Try something like this (untested): > > > > # replace with your function that inputs a data frame and outputs a number > > F <- function(x) length(x) > > # replace with setwd to appropriate path > > setwd("/") > > > > filenames <- dir(pattern = "[.]csv$") > > # DFs is a list of data frames; names(DFs) will be the filenames > > DFs <- sapply(filenames, read.csv, simplify = FALSE) > > > > m <- matrix(0, 26, 12) > > rownames(m) <- LETTERS > > > > for(nam in filenames) { > > row <- substring(nam, 1, 1) > > col <- as.numeric(substring(nam, 2, 3)) > > m[row, col] <- F(DFs[[nam]]) > > } > > > > The for loop could alternately be done like this: > > > > library(gsubfn) > > f <- function(filename, row, col) m[row, as.numeric(col)] <<- F(DFs[[filename]]) > > The regular expression should be "^(.)(..).*" > > > junk <- strapply(filenames, "^(.)(..)", f) > > > > > > On 10/24/06, Antje <niederlein-rstat at yahoo.de> wrote: > > > Hello, > > > > > > I have the following problem. I have a set of CSV files and they are > > > named for a special position in a matrix (e.g. "A01.csv", "F06.csv", > > > "H11.csv") > > > > > > Now, I would like to read in the data of all these files and eveluate > > > something for each set (and write it at the position in a result matrix). > > > How can I realize this with R? > > > > > > Ciao, > > > Antje > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch 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. > > > > > >
Thanks very much for your help. I found the solution. I created a matrix with Strings and within a loop I read the files and processed them wellInfo <- matrix(nrow = rows, ncol = cols) for(i in 1:rows) { tempRow <- paste(rowString[i], formatC(c(1:cols), flag="0", width=2), sep="") print(tempRow) wellInfo[i,] <- tempRow } for(i in 1:rows) { for(j in 1:cols) { filename <- paste(wellInfo[i,j],".csv",sep="") if( file.exists(filename)) { file <- read.csv(filename, sep="\t", header=F) ... } } } (It was quite simple, but I did not find the "paste" command before...) If there is something which could be done easier, just let me know. Ciao, Antje Antje schrieb:> Hello, > > I have the following problem. I have a set of CSV files and they are > named for a special position in a matrix (e.g. "A01.csv", "F06.csv", > "H11.csv") > > Now, I would like to read in the data of all these files and eveluate > something for each set (and write it at the position in a result matrix). > How can I realize this with R? > > Ciao, > Antje > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >
With the style of whole objects encouraged for R that code looks like this: rowString <- LETTERS[1:3]; cols <- 3 # test data mat <- outer(rowString, formatC(1:cols, flag = "0", width = 2), paste, sep = "") mat <- paste(mat, "csv", sep = ".") sapply(mat, function(el) { if (exists(el)) { DF <- read.table(el, sep ="\t"); ... }}) On 10/25/06, Antje <niederlein-rstat at yahoo.de> wrote:> Thanks very much for your help. > I found the solution. I created a matrix with Strings and within a loop > I read the files and processed them > > wellInfo <- matrix(nrow = rows, ncol = cols) > for(i in 1:rows) { > tempRow <- paste(rowString[i], formatC(c(1:cols), flag="0", > width=2), sep="") > print(tempRow) > wellInfo[i,] <- tempRow > } > > for(i in 1:rows) { > for(j in 1:cols) { > filename <- paste(wellInfo[i,j],".csv",sep="") > if( file.exists(filename)) { > file <- read.csv(filename, sep="\t", header=F) > ... > } > } > } > > (It was quite simple, but I did not find the "paste" command before...) > If there is something which could be done easier, just let me know. > > Ciao, > Antje > > > > Antje schrieb: > > Hello, > > > > I have the following problem. I have a set of CSV files and they are > > named for a special position in a matrix (e.g. "A01.csv", "F06.csv", > > "H11.csv") > > > > Now, I would like to read in the data of all these files and eveluate > > something for each set (and write it at the position in a result matrix). > > How can I realize this with R? > > > > Ciao, > > Antje > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > > > > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >