Dear All, How do I read in multiple data frames or matrices in a loop, e.g. for (i in 1:n) { channel <- odbcConnectExcel("filenames") file[i] <- as.data.frame(sqlFetch(channel, "sheet")) } I would like file[i] to be the name of the data.frame (i.e. file[1], file[2], file[3],...etc) rather than a vector. Thanks in advance for any help. Dave
You're almost there, use a list: myfiles <- list() for (i in 1:n) myfiles[[i]] <- etc You can then get at your data frames with myfiles[[1]], myfiles[[2]]... Or, if you prefer to combine them into a single data frame (assuming they're similar), allmyfiles <- do.call("rbind",myfiles) -----Original Message----- From: Dave Evens [mailto:devens8765 at yahoo.com] Sent: Tuesday, May 24, 2005 11:10 AM To: r-help at stat.math.ethz.ch Subject: [R] reading multiple files Dear All, How do I read in multiple data frames or matrices in a loop, e.g. for (i in 1:n) { channel <- odbcConnectExcel("filenames") file[i] <- as.data.frame(sqlFetch(channel, "sheet")) } I would like file[i] to be the name of the data.frame (i.e. file[1], file[2], file[3],...etc) rather than a vector. Thanks in advance for any help. Dave ______________________________________________ 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
Dave Evens wrote:> Dear All, > > > How do I read in multiple data frames or matrices in a > loop, e.g. > > for (i in 1:n) { > channel <- odbcConnectExcel("filenames") > file[i] <- as.data.frame(sqlFetch(channel, > "sheet")) > } > > I would like file[i] to be the name of the data.frame > (i.e. file[1], file[2], file[3],...etc) rather than a > vector.The terminology you are using re. R objects seems to be rather confused. Quite probably you want a list of data frames such as using the filenames as names for the list elements along the follwoing lines: library(RODBC) dataframes <- vector(mode="list", length=length(filenames)) names(dataframes) <- filenames for(i in filenames){ channel <- odbcConnectExcel(i) dataframes[[i]] <- sqlFetch(channel, "sheet") close(channel) } Uwe Ligges> Thanks in advance for any help. > > Dave > > ______________________________________________ > 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