[R] Dynamic arguments in "rbind" function

Duncan Murdoch murdoch at stats.uwo.ca
Tue Jan 5 01:36:43 CET 2010


On 04/01/2010 7:31 PM, Steven Kang wrote:
> Hi, all
> 
> Basically, I have unknown number of data that need to be imported and
> collapsed row-wisely.
> 
> The code below works fine, however the "rbind" function may require 50
> arguments if there are 50 data files...
> 
> Thus, I would like to explore whether there are any methods in using dynamic
> objects (i.e from the resulting objects in the for loop) as an argument in
> the *"rbind"* function.
> 
> 
> 
> setwd(.........)
> 
> import.files <- c("a.txt", "b.txt",  "c.txt",  "d.txt",  "e.txt")
> for (i in 1:length(import.files)) {
>       assign(paste("imp", i, sep = "."), read.delim(eval(paste(".\\",
> import.files[i], sep = "")), header = TRUE))

Computing names like this is almost always a bad idea.  It's better to 
just put the items in a list:

imp <- list()
for (i in 1:length(import.files)) {
   imp[[i]] <- read.delim(eval(paste(".\\",
   import.files[i], sep = "")), header = TRUE)
}

Then to bind them all, simply use do.call(rbind, imp).

Duncan Murdoch

> }
> 
> combined <- rbind(*imp.1, imp.2, imp.3, imp.4, imp.5, imp.6*)
> 
> 
> 
> Your expertise in resolving this issue would be greatly appreciated.
> 
> 
> 
> Steve
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list