有效地将x/y坐标列表转换为R中的数据帧

问题描述:

我有一个长度为30'000的列表,列表中有数据帧,它们有x和y列。数据帧是稀疏的,所以不存在x的每个值。所有x值都介于1和200之间。有效地将x/y坐标列表转换为R中的数据帧

我想将此列表转换为单个数据框,其中每个可能的x值都有一列,每行应该表示列表条目的所有y值(如果ax值为不存在,条目应该是0)。我有一个可行的解决方案(见下文),但它非常非常慢,我认为必须有一个更快(也可能更优雅的方式)才能这样做。

我当前的解决方案(这是慢)为:

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 
+0

如果我正确地读这篇文章,你可以用这个成语'做.call(rbind,whaledatas')将'data.frames'的'list'转换为一个'data.frame'。 – Justin 2013-03-11 20:45:31

+0

当你说数值在1和200之间时,这些整数值是否只有? – mnel 2013-03-11 22:36:32

这里的一个答案,它利用合理量时间:

# 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是发生聪明!的@ regetz的溶液中的微小变化:

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 

这似乎是速度甚至比@ regetz的(如下图所示):

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

@mnel,感谢'setattr'。我编辑了代码。虽然我看不到性能差异(51秒)。 – Arun 2013-03-11 22:34:01

+0

我使用'do.call(rbind,..)',因为我正在返回一个矢量。 'rbindlist'需要data.frame/data.table/list。我这样做是因为我想直接获得30000 * 200的矩阵。通过执行'rbindlist',我最终得到了一个'data.table'(两列,x和y),我需要从中重新创建一个矩阵。没有性能收益。单独创建data.table需要51秒。 – Arun 2013-03-11 22:38:54

+1

'setattr'避免了一个副本,这是一件好事,它也是瞬时的,所以会用更大的数据进行缩放。我不相信我真的明白这个问题,也许'rbindlist - >重塑到广泛是OP的后面。 – mnel 2013-03-11 22:43:15

我会用一个data.table的解决方案,这样的事情:

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

以及缺失的值如何?他预计30000 * 200数据帧/矩阵。 – Arun 2013-03-11 21:08:06

+0

@Arun我不知道'rbindList'会如何处理NA值。我选择它是因为它比“do.call”快。 – agstudy 2013-03-11 21:15:30

首先,这里是清单的一个小例子数据帧:

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

我觉得这个和是一样的在原来的问题10循环?

# 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 

定时采样数据帧30k的长度列表,生成使用Arun的my_sampler功能:

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 
+0

FWIW,使用Arun非常有用的my_sampler函数 – regetz 2013-03-11 22:37:07

+0

,您可以在30000个数据上运行'system.time(。)'您的代码,这个'datify'函数在 Arun 2013-03-11 22:42:13

+0

@阿伦:谢谢你的建议(和功能)。我在我的答案中添加了时间。 – regetz 2013-03-11 22:51:05