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

Dean Attali daattali at gmail.com
Mon Aug 3 05:30:59 CEST 2015


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



More information about the R-devel mailing list