Dear R users, Are there any generic arrays (arrays which can store arbitrary objects) in R? Something along the lines of f <- function(x) x^2 a <- array(f,c(2,2)) etc. I have found fasp in the spatstat package, but in my particular case, I need an array of polynomials, not functions. I know how to hack this together (polynomials can be represented as vectors, so I just extend the dimension) but I am looking for something more generic, and later I might switch to splines. The numerical problem I am solving is the following: I have an array with four indices (a[i,j,k,l]), and I would like to fit something smooth on of one the dimensions, say l, because I need interpolated/smoothed values for intermediate points. Thanks, Tamas
On Thu, 2 Nov 2006, Tamas K Papp wrote:> Dear R users, > > Are there any generic arrays (arrays which can store arbitrary > objects) in R? Something along the lines ofYes. Arrays are vectors with dim attributes, and lists are generic vectors.> f <- function(x) x^2 > a <- array(f,c(2,2))The error message is> f <- function(x) x^2 > a <- array(f,c(2,2))Error in as.vector(x, mode) : cannot coerce to vector but a <- array(list(f),c(2,2)) a[[2,1]] will work. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Fri, Nov 03, 2006 at 07:20:34AM +0000, Prof Brian Ripley wrote:> but > > a <- array(list(f),c(2,2)) > a[[2,1]]Thank you very much. Can you please tell me how to make apply generate arrays like this? Eg if a <- array(1:16,rep(2,4)) then a apply(a,c(1,2,4), polynomial) will just give me a regular array of numbers, and apply(a,c(1,2,4),function(x) list(polynomial(x))) give me a generic array, but the polynomials I want are inside lists: I need constructs like [[1,1,1]][[1]] to access them. I would like to get a 3-way array with polynomials in it. It seems to work with function(x) splinefun(c(0,1),x) instead of polynomial, but array just converts polynomials into vectors... How can I protect them? Thank you, Tamas