[R] Indexing Elements of a Dataframe
    Peter Dalgaard BSA 
    p.dalgaard at biostat.ku.dk
       
    Tue May 28 22:07:11 CEST 2002
    
    
  
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
    
    
More information about the R-help
mailing list