1

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 
4

3 回答 3

4

这是一个需要合理时间的答案:

# 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))

解决方案:使用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

编辑:正如@regetz 所示,分配最终矩阵,然后用 y 值替换出现 x 的选定条目是聪明的!@regez 解决方案的一个小变化:

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 

这似乎比@regez 更快(如下所示):

> system.time(dat <- datify(in.d, xmax=200))
#   user  system elapsed 
#  2.966   0.015   2.993 
于 2013-03-11T21:37:58.980 回答
1

我会使用一个data.table解决方案,如下所示:

whaledatas <- lapply(1:30000,function(x)data.frame(x=1:200,y=1:200))
library(data.table)
dtt <- rbindlist(whaledatas)
于 2013-03-11T21:05:12.387 回答
1

首先,这是一个数据框列表的小例子:

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

for我认为这与原始问题中的循环做同样的事情?

# 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"]]

编辑

随着输入数量的rbind增加,速度变得非常慢。这个版本(为了方便而包装在一个函数中)避免了它,并且运行得更快:

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
}

请注意,我们从 中的所有零开始dat,因此无需在事后修复它...

> 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

my_sampler使用 Arun函数生成的 30k 长度的样本数据帧列表的计时:

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 
于 2013-03-11T21:15:07.483 回答