I'm looking for a library that is either in R or easily wrappable with R, that can do the following things:

  • construct and subset N-dimensional arrays
  • perform operations such as min, max, sum, sd along any dimension
  • is fast and ideally has a BLAS backend

And (this is important), is able to define multilevel-masks along any dimension (and combinations thereof) where the above operations are summarised. E.g.,

1 3 5
2 4 6   -(rowmask=[1,3])-(colmask=1,2)-(op=sum)-(axis=rows)->  8 11
7 8 9


1 3 5                                                      3 7 11
2 4 6   -(rowlevels=['a','a','b'])-(op=sum)-(axis=rows)->  7 8 9
7 8 9

and ideally a combination of the two as well.

I've started coding this in core R but it gets a mess quickly.

Any suggestions?

  • $\begingroup$ By $n$-dimensional matrices, do you mean order $n$ tensors? (Or $n$-dimensional arrays?) Usually, matrices are 2-dimensional constructs ($m$ rows by $n$ columns). $\endgroup$ Jan 29, 2014 at 22:44
  • $\begingroup$ I meant n-dimensional arrays, should have probably used that word there. $\endgroup$ Jan 30, 2014 at 0:04

3 Answers 3


The DyND library might interest you. It comes out of the Scientific Python ecosystem as a numpy replacement but I believe that it is straight C++ and so should be easily wrappable in R.

Actually, having a single numeric library shared between the two languages might have unintended benefits.


By far the best solution for R is DataTable


I'm not sure about BLAS backend, but DataTable is wickedly fast. I had a job that used aggregation from data.frame that took about 24 hours. I simply replaced everything with data tables and it finished in about 20 minutes. This was 2 years ago when the package was in its infancy.

see timings: http://cran.r-project.org/web/packages/data.table/vignettes/datatable-timings.pdf


The array function in base R provides this functionality:

x <- array(c(1, 2, 7, 3, 4, 8, 5, 6, 9), dim = c(3, 3, 1))

The slam package provides a format for sparse arrays:

s <- as.simple_sparse_array(x)

I haven't ever used slam, so I'm not familiar with the ins and outs. If your arrays are going to be pretty dense, just use R arrays.

Between array indexing and the apply function, you should be able to do most of what you want.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.