'vapply' not returning list element names when returned element is a length-1 list

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

'vapply' not returning list element names when returned element is a length-1 list

Dean Attali
Hi all

Sorry for the confusing title. I noticed the following inconsistency:

If i have a function that returns a named list with 2 (or more) elements,
then using 'vapply' retains the names of the elements:

> vapply(1:3, function(x) list("foo" = "bar", "hello" = "world"),
> vector("list", 2))

      [,1]    [,2]    [,3]

foo   "bar"   "bar"   "bar"

hello "world" "world" "world"


But if the function only returns one element, then the name "foo" is lost



> vapply(1:3, function(x) list("foo" = "bar"), vector("list", 1))

[[1]]

[1] "bar"


> [[2]]

[1] "bar"


> [[3]]

[1] "bar"


 Note that when 'lapply' is used instead, the name IS retained

>
> > lapply(1:3, function(x) list("foo" = "bar"))
> [[1]]
> [[1]]$foo
> [1] "bar"
>
> [[2]]
> [[2]]$foo
> [1] "bar"
>
> [[3]]
> [[3]]$foo
> [1] "bar"



I'm not sure if this is intentional or a bug, but it's made my life more
difficult on several occasions and I don't see any reason it would behave
like this.

Thanks

---
http://deanattali.com

        [[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: 'vapply' not returning list element names when returned element is a length-1 list

S Ellison-2


> -----Original Message-----
> From: R-devel [mailto:[hidden email]] On Behalf Of Dean Attali
> If i have a function that returns a named list with 2 (or more) elements,
> then using 'vapply' retains the names of the elements:
> ....
> But if the function only returns one element, then the name "foo" is lost

vapply _always simplifies_ according to the documentation.

In the first case (function return value contains more than one element, and each ), vapply simplifies to a matrix of two lists (!).  The names "foo" and "hello" have been added to the dimnames so you can tell which is which.

in the second case the function return value is a single list and not a matrix of lists (a simple list is simpler than a matrix of lists). The name of the list ('foo') has nowhere to go; instead, you would be assigning the list to a named variable and you don't need the name 'foo'.

Whether that is inconsistent is something of a matter of perspective. Simplification applied as far as possible will always depend on what simplification is possible for the particular return values, so different return values provide different behaviour.

S Ellison


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

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

Re: 'vapply' not returning list element names when returned element is a length-1 list

Dean Attali
Thank you, I do see that now. I still don't completely agree with the
behaviour, I hoped there would be a way to not simplify, but thank you for
pointing me to the fact that it is documented.

---
http://deanattali.com

On 5 August 2015 at 05:08, S Ellison <[hidden email]> wrote:

>
>
> > -----Original Message-----
> > From: R-devel [mailto:[hidden email]] On Behalf Of Dean
> Attali
> > If i have a function that returns a named list with 2 (or more) elements,
> > then using 'vapply' retains the names of the elements:
> > ....
> > But if the function only returns one element, then the name "foo" is lost
>
> vapply _always simplifies_ according to the documentation.
>
> In the first case (function return value contains more than one element,
> and each ), vapply simplifies to a matrix of two lists (!).  The names
> "foo" and "hello" have been added to the dimnames so you can tell which is
> which.
>
> in the second case the function return value is a single list and not a
> matrix of lists (a simple list is simpler than a matrix of lists). The name
> of the list ('foo') has nowhere to go; instead, you would be assigning the
> list to a named variable and you don't need the name 'foo'.
>
> Whether that is inconsistent is something of a matter of perspective.
> Simplification applied as far as possible will always depend on what
> simplification is possible for the particular return values, so different
> return values provide different behaviour.
>
> S Ellison
>
>
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:12}}

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

Re: 'vapply' not returning list element names when returned element is a length-1 list

R devel mailing list
In reply to this post by Dean Attali
Quote----------------

> If i have a function that returns a named list with 2 (or more) elements,
> then using 'vapply' retains the names of the elements:
> ....
> But if the function only returns one element, then the name "foo" is lost

vapply _always simplifies_ according to the documentation.

In the first case (function return value contains more than one element, and each ), vapply simplifies to a matrix of two lists (!).  The names "foo" and "hello" have been added to the dimnames so you can tell which is which.

in the second case the function return value is a single list and not a matrix of lists (a simple list is simpler than a matrix of lists). The name of the list ('foo') has nowhere to go; instead, you would be assigning the list to a named variable and you don't need the name 'foo'.

Whether that is inconsistent is something of a matter of perspective. Simplification applied as far as possible will always depend on what simplification is possible for the particular return values, so different return values provide different behaviour.

S Ellison

--------------------------------------

In the first case, the result is a matrix of mode list, which is a list with "dim" attribute of length 2.

For comparison, 'sapply' retains the name "foo".

> sapply(1:3, function(x) list("foo" = "bar"))
$foo
[1] "bar"

$foo
[1] "bar"

$foo
[1] "bar"

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