Hello List-Members,
Let's say that I have the following code:
for (i in c(10, 20, 30)) {
for (j in c(200, 400, 600)) {
...
...
x <- "something"
* (code here)
}
}
* Now, x is some result that I want to put into a results "matrix"
that
looks like this:
200 400 600
10
20
30
I came up with an ad-hoc solution adding some counters (called "ro"
and
"co") and using a matrix for the results, which I could index with
those
counters (results[ro,co] <- x) and then converting that matrix into a
dataframe and changing the row and column names. That seems a little too
convoluted.
I tried to come up with a "neater" solution, trying to create a
dataframe that looks like the one above and then indexing the fields in
the dataframe by results$i[j] but I couldn't get it to work. Is there a
way of doing this or maybe a better solution?
Thanks in advance!
Wolfgang
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Wolfgang Viechtbauer <wviechtb at s.psych.uiuc.edu> writes:> Hello List-Members, > > Let's say that I have the following code: > > for (i in c(10, 20, 30)) { > for (j in c(200, 400, 600)) { > ... > ... > x <- "something" > * (code here) > } > } > > * Now, x is some result that I want to put into a results "matrix" that > looks like this: > > 200 400 600 > 10 > 20 > 30 > > I came up with an ad-hoc solution adding some counters (called "ro" and > "co") and using a matrix for the results, which I could index with those > counters (results[ro,co] <- x) and then converting that matrix into a > dataframe and changing the row and column names. That seems a little too > convoluted. > > I tried to come up with a "neater" solution, trying to create a > dataframe that looks like the one above and then indexing the fields in > the dataframe by results$i[j] but I couldn't get it to work. Is there a > way of doing this or maybe a better solution?The canonical way is f <- function(i,j) <<something>> ilist <- c(10,20,30) jlist <- c(200,400,600) names(ilist) <- ilist names(jlist) <- jlist outer(ilist, jlist, f) Notice that f should be vectorized, i.e. accept vectors for i and j. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hello Wolfgang,
Tuesday, May 28, 2002, 8:40:13 PM, you wrote:
WV> Hello List-Members,
WV> Let's say that I have the following code:
WV> for (i in c(10, 20, 30)) {
WV> for (j in c(200, 400, 600)) {
WV> ...
WV> ...
WV> x <- "something"
WV> * (code here)
WV> }
WV> }
WV> * Now, x is some result that I want to put into a results
"matrix" that
WV> looks like this:
WV> 200 400 600
WV> 10
WV> 20
WV> 30
WV> I came up with an ad-hoc solution adding some counters (called
"ro" and
WV> "co") and using a matrix for the results, which I could index
with those
WV> counters (results[ro,co] <- x) and then converting that matrix into a
WV> dataframe and changing the row and column names. That seems a little too
WV> convoluted.
WV> I tried to come up with a "neater" solution, trying to create a
WV> dataframe that looks like the one above and then indexing the fields in
WV> the dataframe by results$i[j] but I couldn't get it to work. Is there
a
WV> way of doing this or maybe a better solution?
WV> Thanks in advance!
WV> Wolfgang
It depends on what you're going to do . That is, how the 'x'
elements of the
resulting matrix are computed, are they only a functions of those vectors being
indexed?. If so, I would consider using outer() function (see help), it is a one
of many functions which allow, among other things, "implicit looping".
Please give some more information.
--
Best regards,
Michal Bojanowski mailto:bojaniss at poczta.onet.pl
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Wolfgang,
I'm not sure exactly what you want to do, or why the result needs to be a
data frame, but what about something like the following?
> rows <- c(10, 20, 30)
> cols <- c(200, 400, 600)
> df <- as.data.frame(outer(rows, cols, function(r, c) r + c))
> row.names(df) <- letters[1:3]
> names(df) <- LETTERS[1:3]
> df
A B C
a 210 410 610
b 220 420 620
c 230 430 630
Or perhaps you want the elements of rows and cols as row and column names:
> row.names(df) <- rows
> names(df) <- cols
> df
200 400 600
10 210 410 610
20 220 420 620
30 230 430 630
Of course, you'd replace the function argument to outer with an appropriate
function.
I hope that this helps,
John
At 01:40 PM 5/28/2002 -0500, Wolfgang Viechtbauer wrote:
>Let's say that I have the following code:
>
>for (i in c(10, 20, 30)) {
> for (j in c(200, 400, 600)) {
> ...
> ...
> x <- "something"
> * (code here)
> }
>}
>
>* Now, x is some result that I want to put into a results "matrix"
that
>looks like this:
>
> 200 400 600
>10
>20
>30
>
>I came up with an ad-hoc solution adding some counters (called
"ro" and
>"co") and using a matrix for the results, which I could index with
those
>counters (results[ro,co] <- x) and then converting that matrix into a
>dataframe and changing the row and column names. That seems a little too
>convoluted.
>
>I tried to come up with a "neater" solution, trying to create a
>dataframe that looks like the one above and then indexing the fields in
>the dataframe by results$i[j] but I couldn't get it to work. Is there a
>way of doing this or maybe a better solution?
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks to all the people who replied to my question. After trying out some of the suggestions, it seems that, in general, using outer() is really the most sensible way of going. Another suggestion was to use: x[as.character(i), as.character(j)] <- "something" which works as well. Again, thanks to all that replied! Wolfgang -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._