[R] Writing a single output file

Hadley Wickham hadley at rice.edu
Thu Dec 23 17:28:30 CET 2010


>> input <- do.call(rbind, lapply(fileNames, function(.name){
> +     .data <- read.table(.name, header = TRUE, as.is = TRUE)
> +     # add file name to the data
> +     .data$file <- .name
> +     .data
> + }))

You can simplify this a little with plyr:

fileNames <- list.files(pattern = "file.*.csv")
names(fileNames) <- fileNames

input <- ldply(fileNames, read.table, header = TRUE, as.is = TRUE)

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list