Dear R community, I have a general question regarding indexing in multidiemensional arrays. Imagine I have a three dimensional array and I only want to extract on vector along a single dimension from it: data <- array(rnorm(64),dim=c(4,4,4)) result <- data[1,1,] If I want to extract more than one of these vectors, it would now really help me to supply a logical matrix of the size of the first two dimensions: indices <- matrix(FALSE,ncol=4,nrow=4) indices[1,3] <- TRUE indices[4,1] <- TRUE result <- data[indices,] This, however would give me an error. I am used to this kind of indexing from Matlab and was wonderingt whether there exists an easy way to do this in R without supplying complicated index matrices of all three dimensions or logical vectors of the size of the whole matrix? The only way I could imagine would be to: result <- data[rep(as.vector(indices),times=4)] but this seems rather complicated and also depends on the order of the dimensions I want to extract. I do not want R to copy Matlabs behaviour, I am just wondering whether I missed one concept of indexing in R? Thanks a lot Jannis
On 11-08-01 5:38 AM, Jannis wrote:> Dear R community, > > > I have a general question regarding indexing in multidiemensional arrays. > > Imagine I have a three dimensional array and I only want to extract on > vector along a single dimension from it: > > > data<- array(rnorm(64),dim=c(4,4,4)) > > result<- data[1,1,] > > If I want to extract more than one of these vectors, it would now really > help me to supply a logical matrix of the size of the first two dimensions: > > > indices<- matrix(FALSE,ncol=4,nrow=4) > indices[1,3]<- TRUE > indices[4,1]<- TRUE > > result<- data[indices,] > > This, however would give me an error. I am used to this kind of indexing > from Matlab and was wonderingt whether there exists an easy way to do > this in R without supplying complicated index matrices of all three > dimensions or logical vectors of the size of the whole matrix? > > The only way I could imagine would be to: > > result<- data[rep(as.vector(indices),times=4)] > > but this seems rather complicated and also depends on the order of the > dimensions I want to extract. > > > I do not want R to copy Matlabs behaviour, I am just wondering whether I > missed one concept of indexing in R? >Base R doesn't have anything like that as far as I know. The closest is matrix indexing: you construct a 3 column matrix whose rows are the indices of each element you want to extract. Possibly plyr or some other package has functions to do this. Duncan Murdoch
What do you think about this? apply(data, 3, '[', indices) On Mon, Aug 1, 2011 at 4:38 AM, Jannis <bt_jannis@yahoo.de> wrote:> Dear R community, > > > I have a general question regarding indexing in multidiemensional arrays. > > Imagine I have a three dimensional array and I only want to extract on > vector along a single dimension from it: > > > data <- array(rnorm(64),dim=c(4,4,4)) > > result <- data[1,1,] > > If I want to extract more than one of these vectors, it would now really > help me to supply a logical matrix of the size of the first two dimensions: > > > indices <- matrix(FALSE,ncol=4,nrow=4) > indices[1,3] <- TRUE > indices[4,1] <- TRUE > > result <- data[indices,] > > This, however would give me an error. I am used to this kind of indexing > from Matlab and was wonderingt whether there exists an easy way to do this > in R without supplying complicated index matrices of all three dimensions or > logical vectors of the size of the whole matrix? > > The only way I could imagine would be to: > > result <- data[rep(as.vector(indices),**times=4)] > > but this seems rather complicated and also depends on the order of the > dimensions I want to extract. > > > I do not want R to copy Matlabs behaviour, I am just wondering whether I > missed one concept of indexing in R? > > > > Thanks a lot > Jannis > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
R. Michael Weylandt <michael.weylandt@gmail.com>
2011-Aug-03 22:12 UTC
[R] General indexing in multidimensional arrays
> This might be a little late: but how about this (slightly clumsy) function: > > putValues <- function(Insert, Destination, Location) { > Location = as.matrix(Location) > Location = array(Location,dim(Destination)) > Destination[Location] <- Insert > return(Destination) > } > > It currently assumes that the location array lines up in dimension order, > but other than that seems to work pretty well. If you want, it shouldn't be > hard to change it to take in a set of dimensions to arrange Location along. > If you like any of the other suggested behaviors, you could put in a > is.null(Insert) option that returns the desired subset of values. I haven't > tested it completely, but for a few sample inputs, it seems be do as > desired. > > Michael > > > On Wed, Aug 3, 2011 at 5:00 PM, Jannis <bt_jannis@yahoo.de> wrote: > >> Thanks for all the replies!Unfortunately the solutions only work for >> extracting subsets of the data (which was exactly what I was asking for) and >> not to replace subsets with other values. I used them, however, to program a >> rather akward function to do that. Seems I found one of the few aspects >> where Matlab actually is slightly easier to use than R. >> >> >> Thanks for your help! >> Jannis >> >> On 08/01/2011 05:50 PM, Gene Leynes wrote: >> >>> What do you think about this? >>> >>> apply(data, 3, '[', indices) >>> >>> >>> On Mon, Aug 1, 2011 at 4:38 AM, Jannis<bt_jannis@yahoo.de> wrote: >>> >>> Dear R community, >>>> >>>> >>>> I have a general question regarding indexing in multidiemensional >>>> arrays. >>>> >>>> Imagine I have a three dimensional array and I only want to extract on >>>> vector along a single dimension from it: >>>> >>>> >>>> data<- array(rnorm(64),dim=c(4,4,4)) >>>> >>>> result<- data[1,1,] >>>> >>>> If I want to extract more than one of these vectors, it would now really >>>> help me to supply a logical matrix of the size of the first two >>>> dimensions: >>>> >>>> >>>> indices<- matrix(FALSE,ncol=4,nrow=4) >>>> indices[1,3]<- TRUE >>>> indices[4,1]<- TRUE >>>> >>>> result<- data[indices,] >>>> >>>> This, however would give me an error. I am used to this kind of indexing >>>> from Matlab and was wonderingt whether there exists an easy way to do >>>> this >>>> in R without supplying complicated index matrices of all three >>>> dimensions or >>>> logical vectors of the size of the whole matrix? >>>> >>>> The only way I could imagine would be to: >>>> >>>> result<- data[rep(as.vector(indices),****times=4)] >>>> >>>> but this seems rather complicated and also depends on the order of the >>>> dimensions I want to extract. >>>> >>>> >>>> I do not want R to copy Matlabs behaviour, I am just wondering whether I >>>> missed one concept of indexing in R? >>>> >>>> >>>> >>>> Thanks a lot >>>> Jannis >>>> >>>> ______________________________****________________ >>>> R-help@r-project.org mailing list >>>> https://stat.ethz.ch/mailman/****listinfo/r-help<https://stat.ethz.ch/mailman/**listinfo/r-help> >>>> <https://stat.**ethz.ch/mailman/listinfo/r-**help<https://stat.ethz.ch/mailman/listinfo/r-help> >>>> > >>>> PLEASE do read the posting guide http://www.R-project.org/** >>>> posting-guide.html<http://www.**R-project.org/posting-guide.**html<http://www.R-project.org/posting-guide.html> >>>> > >>>> and provide commented, minimal, self-contained, reproducible code. >>>> >>>> >> ______________________________**________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >> PLEASE do read the posting guide http://www.R-project.org/** >> posting-guide.html <http://www.R-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]