Web pages of Lars Relund Nielsen

Using an array for storing an object in R

Often you need fast access/lookup in R. For instance assume that you have the following data frame/matrix dat

>head(dat)
     x y z
[1,] 1 1 1  0.46 -0.89  0.88 -0.21 0.46  0.93 1.06
[2,] 1 1 2  0.72 -0.40  1.71 -0.52 0.95    NA   NA
[3,] 1 1 3 -0.49 -0.42 -0.26 -0.06 1.07  1.48 1.08
[4,] 1 1 4 -1.55 -0.90  0.15 -0.60 1.86 -1.15   NA
[5,] 1 1 5 -0.46 -1.54 -0.40    NA   NA    NA   NA
[6,] 1 1 6  0.14  1.13 -2.42  0.86 0.13 -1.60 0.62

x, y and z represent an unique index and the rest of the columns represent a vector. The vectors may be of various length. To find the vector corresponding to index (x,y,z) you can do a normal search:

> x<-1
> y<-1
> z<-5
> tmp<-dat[dat[,1]==x & dat[,2]==y & dat[,3]==z,4:10]
> tmp[which(!is.na(tmp))]
[1] -0.46 -1.54 -0.40

However, if dat is big this approach may be to slow. Another approach may be to store the vectors in an array instead:

lookup<-array(list(),c(max(dat[,1]),max(dat[,2]),max(dat[,3])))   # create an array of empty lists
addVec<-function(x) {
   tmp<-as.numeric(x[4:10])
   lookup[[x[1],x[2],x[3]]]<<-tmp[which(!is.na(tmp))]
   invisible(NULL)
}
apply(dat,1,addVec)
lookup[[x,y,z]]		# make a direct lookup in the array
[1] -0.46 -1.54 -0.40

Note that all kinds of objects can be stored in the array since you may store listes too.

Leave a Reply