I want to read data from a number of files into R. Reading individual files one by one requires writing enormous amount of code that will look something like the following. **************** maptools:::dbf.read("wb-01vc.dbf")->dist1 maptools:::dbf.read("wb-02vc.dbf")->dist2 maptools:::dbf.read("wb-03vc.dbf")->dist3 maptools:::dbf.read("wb-04vc.dbf")->dist4 maptools:::dbf.read("wb-05vc.dbf")->dist5 maptools:::dbf.read("wb-06vc.dbf")->dist6 maptools:::dbf.read("wb-07vc.dbf")->dist7 maptools:::dbf.read("wb-08vc.dbf")->dist8 maptools:::dbf.read("wb-09vc.dbf")->dist9 ***************** Is there a better way of doing this? Vikas
Hi! There is a function ?dir which returns you the content of the dir_ectory. If this is more then there is a function ?grep which allows you to extract relevant items. If you need to postprocess the names you have a function ?paste for example. And finally you have an S language construct for(){} And there is help.search() and An Introduction to R to which tells you how to write functions. /E Vikas Rawal wrote:> I want to read data from a number of files into R. > Reading individual files one by one requires writing enormous amount > of code that will look something like the following. > > **************** > maptools:::dbf.read("wb-01vc.dbf")->dist1 > maptools:::dbf.read("wb-02vc.dbf")->dist2 > maptools:::dbf.read("wb-03vc.dbf")->dist3 > maptools:::dbf.read("wb-04vc.dbf")->dist4 > maptools:::dbf.read("wb-05vc.dbf")->dist5 > maptools:::dbf.read("wb-06vc.dbf")->dist6 > maptools:::dbf.read("wb-07vc.dbf")->dist7 > maptools:::dbf.read("wb-08vc.dbf")->dist8 > maptools:::dbf.read("wb-09vc.dbf")->dist9 > ***************** > > Is there a better way of doing this? > > Vikas > > ______________________________________________ > 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 >-- Dipl. bio-chem. Witold Eryk Wolski MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin _ tel: 0049-30-83875219 'v' http://www.molgen.mpg.de/~wolski / \ mail: witek96 at users.sourceforge.net ---W-W---- wolski at molgen.mpg.de
On Fri, 1 Oct 2004, Vikas Rawal wrote:> I want to read data from a number of files into R. > Reading individual files one by one requires writing enormous amount of > code that will look something like the following. > > **************** > maptools:::dbf.read("wb-01vc.dbf")->dist1 > maptools:::dbf.read("wb-02vc.dbf")->dist2 > maptools:::dbf.read("wb-03vc.dbf")->dist3 > maptools:::dbf.read("wb-04vc.dbf")->dist4 > maptools:::dbf.read("wb-05vc.dbf")->dist5 > maptools:::dbf.read("wb-06vc.dbf")->dist6 > maptools:::dbf.read("wb-07vc.dbf")->dist7 > maptools:::dbf.read("wb-08vc.dbf")->dist8 > maptools:::dbf.read("wb-09vc.dbf")->dist9 > ***************** >In this case, you could pre-allocate a list and: res <- vector(mode="list", length=9) for (i in 1:length(res)) res[[i]] <- maptools:::dbf.read(paste("wb-0", i, "vc.dbf", sep=""))> res <- vector(mode="list", length=9) > for (i in 1:length(res)) cat(paste("wb-0", i, "vc.dbf", sep=""), "\n")wb-01vc.dbf wb-02vc.dbf wb-03vc.dbf ... gives a check on what file names are being used. For 10 to 99 preserving the 01-09, use paste("wb-", formatC(i, width=2, flag="0"), "vc.dbf", sep=""). If the token is a character (string) that varies, you can roll out a character vector of tokens first and step along it.> res <- vector(mode="list", length=length(LETTERS)) > for (i in 1:length(res)) cat(paste("wb-", LETTERS[i], "vc.dbf", sep=""),+ "\n") wb-Avc.dbf wb-Bvc.dbf wb-Cvc.dbf ...> Is there a better way of doing this? > > Vikas > > ______________________________________________ > 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 >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
Hoi Vikas, --On vrijdag 1 oktober 2004 10:50 +0530 Vikas Rawal <vikas at mail.jnu.ac.in> wrote:> I want to read data from a number of files into R. > Reading individual files one by one requires writing enormous amount of > code that will look something like the following. > > Is there a better way of doing this? >These days I'm using the code below to read in each datafile I have, and come out with a single dataframe. # Concatenate the raw data files. (datafiles <- list.files(path="../raw data/", pattern="pp.+\.dat$")) tst <- do.call('rbind', lapply(datafiles, function(x) read.table( paste('../raw data/', x, sep=""), skip=1))) rm(datafiles) -- Paul Lemmens NICI, University of Nijmegen ASCII Ribbon Campaign /"\ Montessorilaan 3 (B.01.05) Against HTML Mail \ / NL-6525 HR Nijmegen X The Netherlands / \ Phonenumber +31-24-3612648 Fax +31-24-3616066