tags:

views:

52

answers:

1

I have a data.frame which I would like to convert to a list by rows, meaning each row would correspond to its own list elements. In other words, I would like a list that is as long as the data.frame has rows.

So far, I've tackled this problem in the following manner, but I was wondering if there's a better way to approach this.

xy.df <- data.frame(x = runif(10),  y = runif(10))

# pre-allocate a list and fill it with a loop
xy.list <- vector("list", nrow(xy.df))
for (i in 1:nrow(xy.df)) {
    xy.list[[i]] <- xy.df[i,]
}
+2  A: 

Eureka!

xy.list <- as.list(as.data.frame(t(xy.df)))
Roman Luštrik
Beat me ;-) . Still, if you'd like just to loop over these values, better use apply.
mbq
Care to demonstrate how to use apply?
Roman Luštrik