[R] Indexing Elements of a Dataframe

Michal Bojanowski bojaniss at poczta.onet.pl
Tue May 28 22:47:54 CEST 2002


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



More information about the R-help mailing list