Thanks Kevin and Roger. This gave me the clue and was a great help.
I have been trying it out. There is some problem in the code that still needs to be figured out.


For the first 9 files, paste("wb-0", i, "vc.dbf", sep="") works fine.
But as you rightly guessed, I have more files.
So when I use paste("wb-", formatC(i, width=2, flag="0"), "vc.dbf", sep=""), dbf.read does not work.
formatC works find if I use it in cat(paste.................). It displays the file names correctly.
But when I use it in dbf.read, it gives the following error.


***********************
res[[i]] <- maptools:::dbf.read(paste("wb-", format(i, width=2,flag=0), "vc.dbf", sep=""))
Error in maptools:::dbf.read(paste("wb-", format(i, width=2,flag=0), "vc.dbf", sep=""))
Error in maptools:::dbf.read(paste("wb-", format(i, width = 2, flag = 0), :
unable to open DBF file
***********************


Of course, the data files are all right. I can read them individually.

What do you think could be the problem?

Vikas

Kevin Bartz wrote:

Roger Bivand wrote:

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

______________________________________________
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html





Good call. Here's a somewhat more R-ified version:

res <- lapply(paste("wb-", formatC(1:99, width=2, flag="0"), "vc.dbf",
                    sep=""), maptools:::dbf.read)

Kevin




______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to