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 

```
First, here is a small example of a list of data frames:

```
# create some sample data
whaledatas <- list(
    data.frame(x=1:3, y=11:13),
    data.frame(x=6:10, y=16:20)
)

```

I think this does the same thing as the `for` loop in the original question?

```
# combine into single data frame
whaledatas.all <- do.call("rbind", whaledatas)

# change this to 200! kept small here for illustration...
XMAX <- 10

# create output matrix
dat <- matrix(0.0, length(whaledatas), XMAX)

# create index vector for dat rows
i <- rep(1:length(whaledatas), sapply(whaledatas, nrow))

# populate dat
dat[cbind(i, whaledatas.all[["x"]])] <- whaledatas.all[["y"]]

```

**Edit**

The `rbind` gets horrendously slow as the number of the inputs increases. This version (wrapped in a function for convenience) avoids it, and runs much faster:

```
datify <- function(x, xmax=200) {
    dat <- matrix(0.0, length(x), xmax)
    for (i in seq_along(x)) {
        this.df <- x[[i]]
        coords <- cbind(rep(i, nrow(this.df)), this.df[["x"]])
        dat[coords] <- this.df[["y"]]
    }
    dat
}

```

Note that we started with all zeros in `dat`, so no need to fix that after the fact...

```
> datify(whaledatas, xmax=10)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   11   12   13    0    0    0    0    0    0     0
[2,]    0    0    0    0    0   16   17   18   19    20

```

Timing on 30k-length list of sample data frames, generated using Arun's `my_sampler` function:

```
set.seed(99)
in.d <- lapply(1:30000, function(x) my_sampler(x))
system.time(dat <- datify(in.d, xmax=200))
##   user  system elapsed 
##  1.317   0.011   1.328 

```