[R] for loop over dataframe without indices

Gabor Grothendieck ggrothendieck at myway.com
Fri Dec 19 17:48:56 CET 2003



Regarding my problem of how to use a for loop over
the rows of a dataframe without using indices,
several people mentioned using transpose and then
iterating over the columns (which were the rows)
and one person suggested apply(df,1,list);
however, both these solutions coerce the data to
different types.

What I now realize is that the thing that is oddly
missing in R is that you can't do an apply over
the rows of a dataframe (at least not without having
it coerced to an array and the elements coerced to
possibly different types).  The documentation does
point this out.  Its not a bug but its an omission
that seems deserving of being addressed.

Thus I propose that apply be extended to handle
data frames directly.   Any comments on this 
before I send a message to r-devel?


(In terms of my previous posting, with such an apply
one could do:

rows <- function(df) apply( df, 1, function(x)x )
for( v in rows(df) ) ... some statements involving v ...

There is still the limitation, of course, that one can
only _access_ rows of df like this.  One still needs
indices to change them.  

As an aside, should id <- function(x)x and rows, as defined
above, be predefined in R?  id certainly plays a special 
role in mathematics and it seems natural to want to iterate
over rows and not just columns of dataframes.




More information about the R-help mailing list