[Rd] t.data.frame

Gabor Grothendieck ggrothendieck at myway.com
Fri Dec 19 21:03:44 MET 2003


I would like to propose that t.data.frame be changed using the
source code below.

Right now t.data.frame coerces the data frame to a matrix and then
transposes the matrix.

I would like to propose that t.data.frame be changed to:

	t.data.frame <- function( df ) { 
		ll <- NULL
		for( i in 1:nrow(df) ) ll <- append( ll, list(df[i,]) )
		ll 
	}

That would allow one to iterate, using for, over the rows of a 
data frame just as easily as the columns without using indices:

	# iterate over columns of df
	for( v in df ) ... statements involving v ...

	# iterate over rows of df
	for( u in t(df) ) ... statements involving u ...

This is less efficient than 

	for( i in 1:nrows(df) ) ... statements involving df[i,] ...

but its simpler and reduction in complexity can be more important
than raw efficiency.   Furthermore, it does not preclude the use
of the index form just cited.

One could still get the old behavior of t like this:

	t(as.matrix(df))



More information about the R-devel mailing list