Unexpected behavior of '[' in an apply instruction

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unexpected behavior of '[' in an apply instruction

Rui Barradas
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected behavior of '[' in an apply instruction

robin hankin-3
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


<[hidden email]>


On Sat, Feb 13, 2021 at 12:26 AM Rui Barradas <[hidden email]> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected behavior of '[' in an apply instruction

Rui Barradas
Hello,

Yes, although there is an accepted solution, I believe you should post
this solution there. It's a base R solution, what the question asks for.

And thanks, I would have never reminded myself of slice.index.

Rui Barradas

Às 20:45 de 12/02/21, robin hankin escreveu:

> 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
>
>
> <mailto:[hidden email]>
>
>
> On Sat, Feb 13, 2021 at 12:26 AM Rui Barradas <[hidden email]
> <mailto:[hidden email]>> 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
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected behavior of '[' in an apply instruction

Sokol Serguei
Le 12/02/2021 à 22:23, Rui Barradas a écrit :
> Hello,
>
> Yes, although there is an accepted solution, I believe you should post
> this solution there. It's a base R solution, what the question asks for.
>
> And thanks, I would have never reminded myself of slice.index.

There is another approach -- produce a call to `[`() putting there
"required number of commas in their proper places" programmatically.
Even if it does not lead to a very readable expression, I think it
merits to be mentioned.

   x <- array(1:60, dim = c(10, 2, 3))
   ld=length(dim(x))
   i=1 # i.e. the first row but can be a slice 1:5, whatever
   do.call(`[`, c(alist(x, i), alist(,)[rep(1,ld-1)], alist(drop=FALSE)))

Best,
Serguei.

>
> Rui Barradas
>
> Às 20:45 de 12/02/21, robin hankin escreveu:
>> 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
>>
>>
>> <mailto:[hidden email]>
>>
>>
>> On Sat, Feb 13, 2021 at 12:26 AM Rui Barradas <[hidden email]
>> <mailto:[hidden email]>> 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
>>
>>     ______________________________________________
>>     [hidden email] <mailto:[hidden email]> mailing list
>>     https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected behavior of '[' in an apply instruction

Sokol Serguei
Le 12/02/2021 à 23:49, Sokol Serguei a écrit :

> Le 12/02/2021 à 22:23, Rui Barradas a écrit :
>> Hello,
>>
>> Yes, although there is an accepted solution, I believe you should
>> post this solution there. It's a base R solution, what the question
>> asks for.
>>
>> And thanks, I would have never reminded myself of slice.index.
>
> There is another approach -- produce a call to `[`() putting there
> "required number of commas in their proper places" programmatically.
> Even if it does not lead to a very readable expression, I think it
> merits to be mentioned.
>
>   x <- array(1:60, dim = c(10, 2, 3))
>   ld=length(dim(x))
>   i=1 # i.e. the first row but can be a slice 1:5, whatever
>   do.call(`[`, c(alist(x, i), alist(,)[rep(1,ld-1)], alist(drop=FALSE)))

Or slightly shorter:

   do.call(`[`, alist(x, i, ,drop=FALSE)[c(1,2,rep(3,ld-1),4)])

>
> Best,
> Serguei.
>
>>
>> Rui Barradas
>>
>> Às 20:45 de 12/02/21, robin hankin escreveu:
>>> 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
>>>
>>>
>>> <mailto:[hidden email]>
>>>
>>>
>>> On Sat, Feb 13, 2021 at 12:26 AM Rui Barradas <[hidden email]
>>> <mailto:[hidden email]>> 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
>>>
>>>     ______________________________________________
>>>     [hidden email] <mailto:[hidden email]> mailing list
>>>     https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unexpected behavior of '[' in an apply instruction

R devel mailing list
In reply to this post by Sokol Serguei
Just to be different, the premise was that you do not know how many dimensions the array had. But that is easily available using dim() including how many items are in each dimension. So, in principle, you can use a normal indexing method perhaps in a loop to get what you want. Not sexy but doable. You can treat the array x as a vector just like lower level R does and access the contents using the formula it uses.

-----Original Message-----
From: R-devel <[hidden email]> On Behalf Of Sokol Serguei
Sent: Friday, February 12, 2021 5:50 PM
To: [hidden email]
Subject: Re: [Rd] Unexpected behavior of '[' in an apply instruction

Le 12/02/2021 à 22:23, Rui Barradas a écrit :
> Hello,
>
> Yes, although there is an accepted solution, I believe you should post
> this solution there. It's a base R solution, what the question asks for.
>
> And thanks, I would have never reminded myself of slice.index.

There is another approach -- produce a call to `[`() putting there "required number of commas in their proper places" programmatically.
Even if it does not lead to a very readable expression, I think it merits to be mentioned.

   x <- array(1:60, dim = c(10, 2, 3))
   ld=length(dim(x))
   i=1 # i.e. the first row but can be a slice 1:5, whatever
   do.call(`[`, c(alist(x, i), alist(,)[rep(1,ld-1)], alist(drop=FALSE)))

Best,
Serguei.

>
> Rui Barradas
>
> Às 20:45 de 12/02/21, robin hankin escreveu:
>> 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
>>
>>
>> <mailto:[hidden email]>
>>
>>
>> On Sat, Feb 13, 2021 at 12:26 AM Rui Barradas <[hidden email]
>> <mailto:[hidden email]>> 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-synt
>> ax-to-extract-rows-of-an-array
>>
>>     Thanks in advance,
>>
>>     Rui Barradas
>>
>>     ______________________________________________
>>     [hidden email] <mailto:[hidden email]> mailing list
>>     https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel