From h@nk|n@rob|n @end|ng |rom gm@||@com  Fri Feb 12 21:45:46 2021
From: h@nk|n@rob|n @end|ng |rom gm@||@com (robin hankin)
Date: Sat, 13 Feb 2021 09:45:46 +1300
Subject: [Rd] Unexpected behavior of '[' in an apply instruction
In-Reply-To: <a8eac065-c1ea-a165-2d92-c4802b4d0373@sapo.pt>
References: <a8eac065-c1ea-a165-2d92-c4802b4d0373@sapo.pt>
Message-ID: <CAHHjBM5PhQfxw3xSZXxuQAuWqTi83eN=EiuCQC7rtC35bS3R9A@mail.gmail.com>

Rui

> x <- array(runif(60), dim = c(10, 2, 3))
> array(x[slice.index(x,1) %in% 1:5],c(5,dim(x)[-1]))

(I don't see this on stackoverflow; should I post this there too?)  Most of
the magic package is devoted to handling arrays of arbitrary dimensions and
this functionality might be good to include if anyone would find it useful.

HTH

Robin


<hankin.robin at gmail.com>


On Sat, Feb 13, 2021 at 12:26 AM Rui Barradas <ruipbarradas at sapo.pt> wrote:

> Hello,
>
> This came up in this StackOverflow post [1].
>
> If x is an array with n dimensions, how to subset by just one dimension?
> If n is known, it's simple, add the required number of commas in their
> proper places.
> But what if the user doesn't know the value of n?
>
> The example below has n = 3, and subsets by the 1st dim. The apply loop
> solves the problem as expected but note that the index i has length(i) > 1.
>
>
> x <- array(1:60, dim = c(10, 2, 3))
>
> d <- 1L
> i <- 1:5
> apply(x, MARGIN = -d, '[', i)
> x[i, , ]
>
>
> If length(i) == 1, argument drop = FALSE doesn't work as I expected it
> to work, only the other way does:
>
>
> i <- 1L
> apply(x, MARGIN = -d, '[', i, drop = FALSE)
> x[i, , drop = FALSE]
>
>
> What am I missing?
>
> [1]
>
> https://stackoverflow.com/questions/66168564/is-there-a-native-r-syntax-to-extract-rows-of-an-array
>
> Thanks in advance,
>
> Rui Barradas
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



[Rd] Unexpected behavior of '[' in an apply instruction
robin hankin