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.1X1 X2 X3 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0> auto.data.2X1 X2 X3 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0> auto.data.3X1 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.iError: 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
Drescher, Michael (MNR)
2007-Jul-12 23:19 UTC
[R] automatically generating and accessing data frames of varying dimensions
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.1X1 X2 X3 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0> auto.data.2X1 X2 X3 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0> auto.data.3X1 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.iError: 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
Is this what you want to do:> auto.length <- c(12,15,6) > for(i in 1:3) {+ nam <- paste("auto.data",i, sep=".") + assign(nam, as.data.frame(matrix(1:auto.length[i], ncol=3))) + }> auto.data.1V1 V2 V3 1 1 5 9 2 2 6 10 3 3 7 11 4 4 8 12> auto.data.2V1 V2 V3 1 1 6 11 2 2 7 12 3 3 8 13 4 4 9 14 5 5 10 15> # output the data > for(i in 1:3){+ cat(x <- paste('auto.data.', i, sep=''), '\n') + print(get(x)) + } auto.data.1 V1 V2 V3 1 1 5 9 2 2 6 10 3 3 7 11 4 4 8 12 auto.data.2 V1 V2 V3 1 1 6 11 2 2 7 12 3 3 8 13 4 4 9 14 5 5 10 15 auto.data.3 V1 V2 V3 1 1 3 5 2 2 4 6>On 7/12/07, Drescher, Michael (MNR) <michael.drescher at ontario.ca> 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 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 > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Hi All, I want to test an H0 hypothesis about the proportions of observed counts in k classes. I know that I can do this with the chisq.test. However, besides of the overall acceptance or rejection of the H0, I would like to know which of the k classes cause(s) rejection and I would like to know the observation-based confidence envelopes for the proportions for the k classes. My quick-and-dirty approach thus far is to do an initial chisq.test on the original k classes and then to lump data into two classes (=one of the original classes and all other original classes lumped into one new class) and do a binom.test. I interpret the result of the binom.test as indicating whether the current class might be the reason for the rejection of the overall H0. Additionally, it gives me a confidence envelope for this class. This approach seems fairly straightforward, but I just do not feel totally comfortable with it. I would feel so much better if there was something like a multinom.test, but to my knowledge there is none. Do you have any suggestions what I could rather do? For instance, I might follow a Monte Carlo-like approach: I simulate proportions for the k classes based on the proportions of observed counts with rmultinom. After exclusion of the most extreme values I construct my confidence envelope based on the remaining simulated proportions. Based on whether the hypothesized proportions fall into the observation-based confidence envelopes, I accept or reject. Do you think that either of these approaches is better or would you suggest doing something totally different? All comments and suggestions are highly appreciated. Kind regards, Michael PS: I guess my request parallels that of Matthias Schmidt from Apr 5, 2004, that was answered by Brian Ripley ... 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