From daattali at gmail.com  Mon Aug  3 05:30:59 2015
From: daattali at gmail.com (Dean Attali)
Date: Sun, 2 Aug 2015 20:30:59 -0700
Subject: [Rd] 'vapply' not returning list element names when returned
 element is a length-1 list
Message-ID: <CA+j8Z+=QN4hOyabjaAtd7gBRSY9RGkXXwRY6uKS_2DoXTp_e_w@mail.gmail.com>

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]]



[Rd] 'vapply' not returning list element names when returned element is a length-1 list
Dean Attali