Define an array> v<-1:256 > dim(v)<-rep(4,4)Subarrays can be obtained as follows:> v[3,2,,2][1] 71 87 103 119> v[3,,,2][,1] [,2] [,3] [,4] [1,] 67 83 99 115 [2,] 71 87 103 119 [3,] 75 91 107 123 [4,] 79 95 111 127 In the general case this procedure is very tedious. Given an array A, dim(A)=(dim_1,dim_2,...,dim_d) and two vectors v1=(n_i1,...n_ik), v2=(int_1,...,int_k) ('marginals' and relevant 'interval numbers') is there a smart way to obtain A[,...,int_1,....,int_2,....,....,int_k,....] ? Best wishes Gunnar Hellmund
Here's one way: > subarray <- function(x, marginals, intervals) { + if (length(marginals) != length(intervals)) + stop("marginals and intervals must be the same length (intervals can be a list)") + if (any(marginals<1 | marginals>length(dim(x)))) + stop("marginals must contain values in 1:length(dim(x))") + ic <- Quote(x[, drop=T]) + # ic has 4 elts with one empty index arg + ic2 <- ic[c(1, 2, rep(3, length(dim(x))), 4)] + # ic2 has an empty arg for each dim of x + ic2[marginals+2] <- intervals + eval(ic2) > } > subarray(v, c(1,4), c(3,2)) [,1] [,2] [,3] [,4] [1,] 67 83 99 115 [2,] 71 87 103 119 [3,] 75 91 107 123 [4,] 79 95 111 127 > subarray(v, c(1,4), list(3,2)) [,1] [,2] [,3] [,4] [1,] 67 83 99 115 [2,] 71 87 103 119 [3,] 75 91 107 123 [4,] 79 95 111 127 > subarray(v, c(1,3,4), list(c(1,3,4),1,2)) [,1] [,2] [,3] [,4] [1,] 65 69 73 77 [2,] 67 71 75 79 [3,] 68 72 76 80 > Question for language experts: is this the best way to create and manipulate R language expressions that contain empty arguments, or are there other preferred ways? -- Tony Plate Gunnar Hellmund wrote:> Define an array > > >>v<-1:256 >>dim(v)<-rep(4,4) > > > Subarrays can be obtained as follows: > > >>v[3,2,,2] > > [1] 71 87 103 119 > >>v[3,,,2] > > [,1] [,2] [,3] [,4] > [1,] 67 83 99 115 > [2,] 71 87 103 119 > [3,] 75 91 107 123 > [4,] 79 95 111 127 > > In the general case this procedure is very tedious. > > Given an array > A, dim(A)=(dim_1,dim_2,...,dim_d) > and two vectors > v1=(n_i1,...n_ik), v2=(int_1,...,int_k) ('marginals' and relevant > 'interval numbers') > is there a smart way to obtain > A[,...,int_1,....,int_2,....,....,int_k,....] > ? > > Best wishes > Gunnar Hellmund > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
You could write an R function to create an integer matrix whose rows are the coordinates of the lattice points in the box with specified lower and upper limits (code at end) makeLattice <- function(lower,upper) { # generate lattice points in box from from lower to upper } for example, then you can create the index matrix via lower <- rep(1,length(dim(A))) lower[v1] <- v2 upper <- dim(A) upper[v1] <- v2 indx <- makeLattice(lower,upper) and now A[indx] is the slice you want. You can dimension it as dim(A)[-v1]. Reid Huntsinger Code: makeLattice <- function(lower,upper) { # generate lattice points in box from from lower to upper n <- length(lower) if (n != length(upper)) stop("vectors must have same length") d <- upper - lower + 1 latticePts <- vector(length=n*prod(d), mode="integer") dim(latticePts) <- c(prod(d), n) # create each column D <- c(1,cumprod(d[-n])) for (j in 1:n) { latticePts[,j] <- as.integer(rep(lower[j]:upper[j],rep(D[j],d[j]))) } latticePts } -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gunnar Hellmund Sent: Friday, April 29, 2005 11:34 AM To: r-help at stat.math.ethz.ch Subject: [R] Subarrays Define an array> v<-1:256 > dim(v)<-rep(4,4)Subarrays can be obtained as follows:> v[3,2,,2][1] 71 87 103 119> v[3,,,2][,1] [,2] [,3] [,4] [1,] 67 83 99 115 [2,] 71 87 103 119 [3,] 75 91 107 123 [4,] 79 95 111 127 In the general case this procedure is very tedious. Given an array A, dim(A)=(dim_1,dim_2,...,dim_d) and two vectors v1=(n_i1,...n_ik), v2=(int_1,...,int_k) ('marginals' and relevant 'interval numbers') is there a smart way to obtain A[,...,int_1,....,int_2,....,....,int_k,....] ? Best wishes Gunnar Hellmund ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html