[R] automatically generating and accessing data frames of varying dimensions

2007-07-12 Thread Drescher, Michael (MNR)
Hi All,

I want to automatically generate a number of data frames, each with an
automatically generated name and an automatically generated number of
rows. The number of rows has been calculated before and is different for
all data frames (e.g. c(4,5,2)). The number of columns is known a priori
and the same for all data frames (e.g. c(3,3,3)). The resulting data
frames could look something like this:

 auto.data.1
  X1 X2 X3
1  0  0  0
2  0  0  0
3  0  0  0
4  0  0  0

 auto.data.2
  X1 X2 X3
1  0  0  0
2  0  0  0
3  0  0  0
4  0  0  0
5  0  0  0

 auto.data.3
  X1 X2 X3
1  0  0  0
2  0  0  0

Later, I want to fill the elements of the data frames with values read
from somewhere else, automatically looping through the previously
generated data frames.

I know that I can automatically generate variables with the right number
of elements with something like this:

 auto.length - c(12,15,6)
 for(i in 1:3) {
+ nam - paste(auto.data,i, sep=.)
+ assign(nam, 1:auto.length[i])
+ }
 auto.data.1
 [1]  1  2  3  4  5  6  7  8  9 10 11 12
 auto.data.2
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
 auto.data.3
[1]  1 2 3 4 5 6

But how do I turn these variables into data frames or give them any
dimensions? Any commands such as 'as.matrix', 'data.frame', or 'dim' do
not seem to work. I also seem not to be able to access the variables
with something like auto.data.i since:

 auto.data.i
Error: object auto.data.i not found

Thus, how would I be able to automatically write to the elements of the
data frames later in a loop such as ...

 for(i in 1:3) {
+ for(j in 1:nrow(auto.data.i)) {   ### this obviously does not work
since 'Error in nrow(auto.data.i) : object auto.data.i not found'
+ for(k in 1:ncol(auto.data.i)) {
+ auto.data.i[j,k] - 'some value'
+ }}}

Thanks a bunch for all your help.

Best, Michael


Michael Drescher
Ontario Forest Research Institute
Ontario Ministry of Natural Resources
1235 Queen St East
Sault Ste Marie, ON, P6A 2E3
Tel: (705) 946-7406
Fax: (705) 946-2030

__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] automatically generating and accessing data frames of varying dimensions

2007-07-12 Thread Julian Burgos
Replace

assign(nam, 1:auto.length[i])

with


assign(nam,data.frame(matrix(1:auto.length[i], ncol=3)))

Verify if the matrix (and hence the data frame) is filled the way you 
intended.  Otherwise, use the argument byrow=T.

Julian M. Burgos

Fisheries Acoustics Research Lab
School of Aquatic and Fishery Science
University of Washington


Drescher, Michael (MNR) wrote:
 Hi All,

 I want to automatically generate a number of data frames, each with an
 automatically generated name and an automatically generated number of
 rows. The number of rows has been calculated before and is different for
 all data frames (e.g. c(4,5,2)). The number of columns is known a priori
 and the same for all data frames (e.g. c(3,3,3)). The resulting data
 frames could look something like this:

 auto.data.1
   X1 X2 X3
 1  0  0  0
 2  0  0  0
 3  0  0  0
 4  0  0  0

 auto.data.2
   X1 X2 X3
 1  0  0  0
 2  0  0  0
 3  0  0  0
 4  0  0  0
 5  0  0  0

 auto.data.3
   X1 X2 X3
 1  0  0  0
 2  0  0  0

 Later, I want to fill the elements of the data frames with values read
 from somewhere else, automatically looping through the previously
 generated data frames.

 I know that I can automatically generate variables with the right number
 of elements with something like this:

 auto.length - c(12,15,6)
 for(i in 1:3) {
 + nam - paste(auto.data,i, sep=.)
 + assign(nam, 1:auto.length[i])
 + }
 auto.data.1
  [1]  1  2  3  4  5  6  7  8  9 10 11 12
 auto.data.2
  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
 auto.data.3
 [1]  1 2 3 4 5 6

 But how do I turn these variables into data frames or give them any
 dimensions? Any commands such as 'as.matrix', 'data.frame', or 'dim' do
 not seem to work. I also seem not to be able to access the variables
 with something like auto.data.i since:

 auto.data.i
 Error: object auto.data.i not found

 Thus, how would I be able to automatically write to the elements of the
 data frames later in a loop such as ...

 for(i in 1:3) {
 + for(j in 1:nrow(auto.data.i)) { ### this obviously does not work
 since 'Error in nrow(auto.data.i) : object auto.data.i not found'
 + for(k in 1:ncol(auto.data.i)) {
 + auto.data.i[j,k] - 'some value'
 + }}}

 Thanks a bunch for all your help.

 Best, Michael


 Michael Drescher
 Ontario Forest Research Institute
 Ontario Ministry of Natural Resources
 1235 Queen St East
 Sault Ste Marie, ON, P6A 2E3
 Tel: (705) 946-7406
 Fax: (705) 946-2030

 __
 R-help@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
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] automatically generating and accessing data frames of varying dimensions

2007-07-12 Thread Gabor Grothendieck
Its desirable to put the data frames into a list so they can be easily
iterated over in the future.  Try this:

auto.length - 2:4 # replace with desired row lengths
f - function(names, nr) {
x - rep(0, nr)
data.frame(X1 = x, X2 = x, X3 = x)
}
auto.names - paste(auto.data, seq_along(auto.length), sep = .)
mapply(f, auto.names, auto.length, SIMPLIFY = FALSE)


On 7/12/07, Drescher, Michael (MNR) [EMAIL PROTECTED] wrote:
 Hi All,

 I want to automatically generate a number of data frames, each with an
 automatically generated name and an automatically generated number of
 rows. The number of rows has been calculated before and is different for
 all data frames (e.g. c(4,5,2)). The number of columns is known a priori
 and the same for all data frames (e.g. c(3,3,3)). The resulting data
 frames could look something like this:

  auto.data.1
  X1 X2 X3
 1  0  0  0
 2  0  0  0
 3  0  0  0
 4  0  0  0

  auto.data.2
  X1 X2 X3
 1  0  0  0
 2  0  0  0
 3  0  0  0
 4  0  0  0
 5  0  0  0

  auto.data.3
  X1 X2 X3
 1  0  0  0
 2  0  0  0

 Later, I want to fill the elements of the data frames with values read
 from somewhere else, automatically looping through the previously
 generated data frames.

 I know that I can automatically generate variables with the right number
 of elements with something like this:

  auto.length - c(12,15,6)
  for(i in 1:3) {
 + nam - paste(auto.data,i, sep=.)
 + assign(nam, 1:auto.length[i])
 + }
  auto.data.1
  [1]  1  2  3  4  5  6  7  8  9 10 11 12
  auto.data.2
  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
  auto.data.3
 [1]  1 2 3 4 5 6

 But how do I turn these variables into data frames or give them any
 dimensions? Any commands such as 'as.matrix', 'data.frame', or 'dim' do
 not seem to work. I also seem not to be able to access the variables
 with something like auto.data.i since:

  auto.data.i
 Error: object auto.data.i not found

 Thus, how would I be able to automatically write to the elements of the
 data frames later in a loop such as ...

  for(i in 1:3) {
 + for(j in 1:nrow(auto.data.i)) {   ### this obviously does not work
 since 'Error in nrow(auto.data.i) : object auto.data.i not found'
 + for(k in 1:ncol(auto.data.i)) {
 + auto.data.i[j,k] - 'some value'
 + }}}

 Thanks a bunch for all your help.

 Best, Michael


 Michael Drescher
 Ontario Forest Research Institute
 Ontario Ministry of Natural Resources
 1235 Queen St East
 Sault Ste Marie, ON, P6A 2E3
 Tel: (705) 946-7406
 Fax: (705) 946-2030

 __
 R-help@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
 and provide commented, minimal, self-contained, reproducible code.


__
R-help@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
and provide commented, minimal, self-contained, reproducible code.