Hi, is there a function in R already doing what I try to do below: # Let 'x' be an array with *any* number of dimensions (>=1). x <- array(1:24, dim=c(2,2,3,2)) ... x <- array(1:24, dim=c(4,3,2)) i <- 2:3 ndim <- length(dim(x)) if (ndim == 1) y <- x[i] else if (ndim == 2) y <- x[i,] else if (ndim == 3) y <- x[i,,] else ... and so on. My current solution is ndim <- length(dim(x)) args <- rep(",", ndim) args[1] <- "i" args <- paste(args, collapse="") code <- paste("x[", args, "]", sep="") expr <- parse(text=code) y <- eval(expr) ndim <- length(dim(x)) args <- rep(",", ndim) args[1] <- "i" args <- paste(args, collapse="") code <- paste("x[", args, "]", sep="") expr <- parse(text=code) y <- eval(expr) Is there another way I can do this in R that I have overlooked? /Henrik
You can look at the definition of 'corner' in the public domain area of the Burns Statistics website. It uses 'do.call' on '[' to achieve (sort of) what you want. Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Henrik Bengtsson wrote:>Hi, > >is there a function in R already doing what I try to do below: > ># Let 'x' be an array with *any* number of dimensions (>=1). >x <- array(1:24, dim=c(2,2,3,2)) >... >x <- array(1:24, dim=c(4,3,2)) > >i <- 2:3 > >ndim <- length(dim(x)) >if (ndim == 1) > y <- x[i] >else if (ndim == 2) > y <- x[i,] >else if (ndim == 3) > y <- x[i,,] >else ... > >and so on. My current solution is > >ndim <- length(dim(x)) >args <- rep(",", ndim) >args[1] <- "i" >args <- paste(args, collapse="") >code <- paste("x[", args, "]", sep="") >expr <- parse(text=code) >y <- eval(expr) > >ndim <- length(dim(x)) >args <- rep(",", ndim) >args[1] <- "i" >args <- paste(args, collapse="") >code <- paste("x[", args, "]", sep="") >expr <- parse(text=code) >y <- eval(expr) > >Is there another way I can do this in R that I have overlooked? > >/Henrik > >______________________________________________ >R-devel at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-devel > > > > >
Henrik Bengtsson <hb at maths.lth.se> writes:> Hi, > > is there a function in R already doing what I try to do below: > > # Let 'x' be an array with *any* number of dimensions (>=1). > x <- array(1:24, dim=c(2,2,3,2)) > ... > x <- array(1:24, dim=c(4,3,2)) > > i <- 2:3 > > ndim <- length(dim(x)) > if (ndim == 1) > y <- x[i] > else if (ndim == 2) > y <- x[i,] > else if (ndim == 3) > y <- x[i,,] > else ... > > and so on. My current solution is > > ndim <- length(dim(x)) > args <- rep(",", ndim) > args[1] <- "i" > args <- paste(args, collapse="") > code <- paste("x[", args, "]", sep="") > expr <- parse(text=code) > y <- eval(expr) > > ndim <- length(dim(x)) > args <- rep(",", ndim) > args[1] <- "i" > args <- paste(args, collapse="") > code <- paste("x[", args, "]", sep="") > expr <- parse(text=code) > y <- eval(expr) > > Is there another way I can do this in R that I have overlooked?I think this should work: x <- array(1:24, dim=c(3,2,2,2)) # not c(2,2,3,2).... i <- 2:3 ndim <- length(dim(x)) ix <- as.list(rep(TRUE, ndim)) ix[[1]] <- i do.call("[", c(list(x), ix)) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
I suppose one can make use of slice.index():> array(x[slice.index(x, 1) == 1], dim(x)[-1])[,1] [,2] [1,] 1 13 [2,] 5 17 [3,] 9 21 Andy> From: Henrik Bengtsson > > Hi, > > is there a function in R already doing what I try to do below: > > # Let 'x' be an array with *any* number of dimensions (>=1). > x <- array(1:24, dim=c(2,2,3,2)) > ... > x <- array(1:24, dim=c(4,3,2)) > > i <- 2:3 > > ndim <- length(dim(x)) > if (ndim == 1) > y <- x[i] > else if (ndim == 2) > y <- x[i,] > else if (ndim == 3) > y <- x[i,,] > else ... > > and so on. My current solution is > > ndim <- length(dim(x)) > args <- rep(",", ndim) > args[1] <- "i" > args <- paste(args, collapse="") > code <- paste("x[", args, "]", sep="") > expr <- parse(text=code) > y <- eval(expr) > > ndim <- length(dim(x)) > args <- rep(",", ndim) > args[1] <- "i" > args <- paste(args, collapse="") > code <- paste("x[", args, "]", sep="") > expr <- parse(text=code) > y <- eval(expr) > > Is there another way I can do this in R that I have overlooked? > > /Henrik > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >
Hi everyone apltake(x,1) [where apltake() is part of library(magic)] does this. best wishes Robin On 23 Nov 2005, at 10:50, Henrik Bengtsson wrote:> Hi, > > is there a function in R already doing what I try to do below: > > # Let 'x' be an array with *any* number of dimensions (>=1). > x <- array(1:24, dim=c(2,2,3,2)) > ... > x <- array(1:24, dim=c(4,3,2)) > > i <- 2:3 > > ndim <- length(dim(x)) > if (ndim == 1) > y <- x[i] > else if (ndim == 2) > y <- x[i,] > else if (ndim == 3) > y <- x[i,,] > else ... > > and so on. My current solution is > > ndim <- length(dim(x)) > args <- rep(",", ndim) > args[1] <- "i" > args <- paste(args, collapse="") > code <- paste("x[", args, "]", sep="") > expr <- parse(text=code) > y <- eval(expr) > > ndim <- length(dim(x)) > args <- rep(",", ndim) > args[1] <- "i" > args <- paste(args, collapse="") > code <- paste("x[", args, "]", sep="") > expr <- parse(text=code) > y <- eval(expr) > > Is there another way I can do this in R that I have overlooked? > > /Henrik > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
Apparently Analagous Threads
- arraytake for extracting subarrays from multidimensional arrays
- withAutoprint({ .... }) ?
- How to assign NULL value to pairlist element while keeping it a pairlist?
- How to assign NULL value to pairlist element while keeping it a pairlist?
- How to assign NULL value to pairlist element while keeping it a pairlist?