What do you think about this?

apply(data, 3, '[', indices)


On Mon, Aug 1, 2011 at 4:38 AM, Jannis <bt_jan...@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-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to