Gray_Hound
Gray_Hound

Reputation: 127

Ordered Map / Hash Table in R

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.

Upvotes: 0

Views: 627

Answers (2)

Gray_Hound
Gray_Hound

Reputation: 127

After some testing seems like the easiest way of doing it is :

Using a hash package to convert it using a hash using :

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

And if it needs to get accessed calling result6[[toString(id)]] With the difference in performance being marginal, albeit it's still fairly annoying having to include toString in your code.

Upvotes: 1

Hong Ooi
Hong Ooi

Reputation: 57696

It's not clear exactly what your question is, but judging by the structure of the loop, you probably want

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

rather than result5[[id]] <- getPrices(id).

Upvotes: 0

Related Questions