[R] Dynamic arguments in rbind function

2010-01-04 Thread Steven Kang
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))
}

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@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.


Re: [R] Dynamic arguments in rbind function

2010-01-04 Thread Duncan Murdoch

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@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.


__
R-help@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.


Re: [R] Dynamic arguments in rbind function

2010-01-04 Thread Sundar Dorai-Raj
Use a list instead of assign then do.call(rbind, thelist).

import.files - c(a.txt, b.txt,  c.txt,  d.txt,  e.txt)
imp - vector(list, length(import.files))
for (i in 1:length(import.files)) {
  imp[[i]] - read.delim(import.files[i], sep = , header = TRUE)
}

combined - do.call(rbind, imp)

HTH,

--sundar

On Mon, Jan 4, 2010 at 4:31 PM, Steven Kang stochastick...@gmail.comwrote:

 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))
 }

 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@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.


[[alternative HTML version deleted]]

__
R-help@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.