s-news
[Top] [All Lists]

Re: Importing multiple .xls files?

To: s-news@lists.biostat.wustl.edu
Subject: Re: Importing multiple .xls files?
From: Sarah Henderson <sarah.henderson@ubc.ca>
Date: Sat, 16 Jul 2005 22:45:47 -0800

Thanks to all who replied! The problem has been solved and I can see how the responses are going to be very valuable in my future work with these data. A summary is found below:

_________________________________________________________________

## I created simple Excel files test1.xls and test3.xls in the
## directory in which my working .Data sits.
## Then I imported them in a loop.
## file.exists() allows me to go through the number sequence and lets
## the S-Plus program figure out whether there is a corresponding file.


objects(p="test[12345]")

for (i in 1:5) {
  name <- paste("test", i, sep="")
  name.i <- paste(name, ".xls", sep="")
  if (file.exists(name.i)) assign(name, importData(name.i))
}

objects(p="test[12345]")


Richard Heidberger
_________________________________________________________________

You can define a function to read the data for station 'id':
        read.station <- function(id, directory = ".") {
filename <- paste(directory, "/", "station", id, ".xls", sep="")
                importData(filename, ... any other fixed arguments ...)
        }
and use it as
        data61 <- read.station(61)
If you want to make a list of data frames containing the
data for several stations, say 61, 450, and 785, you can use
        stations <- c(61, 450, 785)
        data <- lapply(stations, read.station)
Then data[[1]] will contain the data for station 61, etc.
You can attach names to data with
        names(data) <- c(stations)
so you can get the data for station 61 with
        data[["61"]]

----------------------------------------------------------------------------
Bill Dunlap
Insightful Corporation
Bill at Insightful dot com
360-428-8146

_________________________________________________________________


1. Produce vectors of character strings with the names of the *.xls files and of the target data.frames you want to create. For example, the following will "station60", "station61", ..., "station800":

          DFname <- paste("station", 60:800, sep="")

You can then delete the ones that don't exist. Then convert this to file names as follows:

          Fname <- paste(DFname, ".xls", sep="")

Alternatively, copy the results of a "dir" into Excel, strip away what you don't want, save the names of the files you want to import, and delete the ".xls" with something like the following:

Period <- regexpr("\\.", Fnames)
DFnames <- substring(Fnames, 1, Period-1)

2. Use the GUI File -> "Import Data" -> "From File" dialog process for the first of the files.

3. Window -> History -> Display -> OK. Then copy the "guiImportData" command into a script file in a loop, replacing the FileName argument with Fname[i] and the TargetDataFrame arguments with DFname[i]. Set i <- 1, and try it until you get it to work. Then try i <- 1+i, and try it again. After you think you have all the options like you want them, you can get the rest in a loop.

          spencer graves





<Prev in Thread] Current Thread [Next in Thread>