[R] List of lists? Data frames? (Or other data structures?)

R A F raf1729 at hotmail.com
Thu May 1 02:51:50 CEST 2003


Hi, I'm faced with the following problem and would appreciate some
advice.

I could have a data frame x that looks like this:
         aa          bb
a        1           "A"
b        2           "B"

The advantage of this is that I could access all the individual
components easily.  Also I could access all the rows and columns
easily.

Alternatively, I could have a list of lists that looks like this:

xprime <- list()
xprime$a <- list()
xprime$b <- list()

xprime$a$aa <- 1
xprime$a$bb <- "A"

xprime$b$aa <- 2
xprime$b$bb <- "B"

etc.

If speed is important, would a list of lists be faster than a data
frame? (I know, for example, that scan is supposed to be faster than
read.table, but I don't know if that is related to issues with data
frames.)

My problem with a list of lists, though, is that if I want to access
all the bb subcomponents, a naive method like this one failed:

y <- c( "a", "b" )
xprime[[ y ]]$bb (Does not work)

So to get all the bb subcomponents I seem to need to loop, which may
slow things down (presumably).  But maybe people here know of a way.

Finally what would be the "best" way given the constraint of quick
access to all rows, columns and individual components?

I'd appreciate your thoughts and comments.  Thanks very much.



More information about the R-help mailing list