0

While working with lists i've noticed an issue that i didn't expect.

result5 <- vector("list",length(queryResults[[1]]))
for(i in 1:length(queryResults[[1]])){
    id <- queryResults[[1]][i]
    result5[[id]] <-getPrices(id)
}

The problem is that after this code runs instead of the result staying the same size (w/e queryResults[[1]] is) it goes up to the last index creating a bunch of null entries in the middle.

result5 current stores a number of int,double lists so it looks like : result5[[index(int)]][[row]][col]

While on it's own it's not too problematic I would rather avoid that simply for easier size calculations later on.

For clarification, id is an integer. And in the given case for loop offers same performance, but greater convenience than the apply functions.

4

2 回答 2

1

经过一些测试似乎最简单的方法是:

使用散列包使用散列转换它:

result6 <- hash(queryResults[[1]],lapply(queryResults[[1]],getPrices))

如果它需要访问调用 result6[[toString(id)]] 性能差异很小,尽管在代码中包含 toString 仍然相当烦人。

于 2013-06-17T15:36:34.210 回答
0

目前尚不清楚您的问题是什么,但从循环的结构来看,您可能想要

result5[[i]] <- getPrices(id)

而不是result5[[id]] <- getPrices(id).

于 2013-06-17T15:05:34.353 回答