Question: I have a list of length 30,000 with data frames in it that have a x and y column. The data frame is sparse, so not each value of x exists. All x values are between 1 and 200.

I want to convert this list to a single data frame which has for each possible x value a column and each row should represent all y values of a list entry (if a x value does not exist, the entry should be 0). I have a solution which works (see below) but it's very, very slow and I think there must be a faster (and probably also more elegant way) to do so.

My current solution (which is slow) is:

```
dat <- matrix(numeric(0), 30000, 200) 
for(i in seq(along=whaledatas)) {
    for(j in row.names(whaledatas[[i]]))
        dat[i, whaledatas[[i]][j,"x"]] <- whaledatas[[i]][j,"y"]
}

dfData <- data.frame(dat, files$label)
dfData[is.na(dfData)] <- 0 

```

Answer: 
Here's an answer that takes reasonable amount of time:

```
# function to create dummy data
my_sampler <- function(idx) {
    x <- sample(200, sample(50:100, 1))
    y <- sample(length(x))
    data.frame(x,y)
}

# create list of 30000 data.frames
in.d <- lapply(1:30000, function(x) my_sampler(x))

```

**Solution:** Using `data.table`

```
require(data.table)
system.time(out.d <- do.call(rbind, lapply(in.d, function(x) {
    setattr(x, 'class', c("data.table", "data.frame")) # mnel's suggestion
    setkey(x, "x")
    x[J(1:200)]$y
})))

#   user  system elapsed 
# 47.111   0.343  51.283 

> dim(out.d)
# [1] 30000   200

# final step: replace NA with 0
out.d[is.na(out.d)] <- 0

```

**Edit:** As @regetz shows, assigning final matrix and then replacing selected entries where x occurs with y-values is clever! A small variation of @regetz's solution:

```
m <- matrix(0.0, nrow=30000, ncol=200)
system.time(for( i in 1:nrow(m)) {
    m[i, in.d[[i]][["x"]]] <- in.d[[i]][["y"]]
})

#   user  system elapsed 
#  1.496   0.003   1.511 

```

This seems to be even faster than @regetz's (shown below):

```
> system.time(dat <- datify(in.d, xmax=200))
#   user  system elapsed 
#  2.966   0.015   2.993 

```
I would use a `data.table` solution , something like this :

```
whaledatas <- lapply(1:30000,function(x)data.frame(x=1:200,y=1:200))
library(data.table)
dtt <- rbindlist(whaledatas)

```