Imlerith Imlerith - 3 months ago 7
R Question

retrieving a list element in O(1) in R

suppose I have the following:

a <- vector('list',50)

for(i in 1:50)
{
a[[i]] <- list(path=paste0("file",sample(0:600,1)),contents=sample(1:5,10*i,replace=TRUE))
}


Now, for example; I want to retrieve the contents of
file45
(assuming it exists in this randomly generated data) as fast as possible.

I have tried the following:

contents <- unlist(Filter(function(x) x$path=="file45",a),recursive=FALSE)$contents


However, the list searching overhead makes reading from memory even slower than reading directly from disk (to some extent).

Is there any other way of retrieving the contents in something reasonably faster than reading from disk ideally O(1) ?

edit: assume that there are no duplicate
filepaths
in my sublists and that there are largely more than 50 sublists

Answer

Use the names attribute to track the items instead:

a <- vector('list',50)

for(i in 1:50)
{
  a[[i]] <- list(contents=sample(1:5,10*i,replace=TRUE))
}

names(a) <- paste0("file",sample(1:600,50))

a[["file45"]]
NULL
a[["file25"]]
$contents
 [1] 3 1 3 1 2 5 1 5 1 2 3 1 4 1 1 4 1 5 1 5 1 4 5 2 5 2 2 5 1 1
Comments